home *** CD-ROM | disk | FTP | other *** search
- \ Lesson 4 Part 13 ( F-PC 3.5 Tutorial by Jack Brown )
- \ Random Case Study. - Visual Demonstration of Random Numbers.
- \ Original by John Somerville, Modified by Jack Brown.
- \ VGA users must also modify SCREEN-PREP and RANDPLOT as shown.
-
- \ Graphics from SMILEY.ZIP or SMILEY35.ZIP
-
- \ FLOAD GRAPHICS \ This line for VGA graphics
- \ Remove comment above and comment the two lines below for VGA cards.
- FLOAD CGA DECIMAL \ Both of these lines
- FLOAD DMULDIV DECIMAL \ for CGA graphics
-
- \ Some standard double aritmetic words built on those in DMULDIV.SEQ
- : UD/MOD ( ud1 ud2 -- udr udq )
- 0. 2SWAP UMD/MOD ;
-
- : UD/ ( ud1 ud2 -- udq )
- UD/MOD 2SWAP 2DROP ;
-
- : UDMOD ( ud1 ud2 -- udr )
- UD/MOD 2DROP ;
-
- \ D. H. Lehmers Parametric multiplicative linear congruential
- \ random number generator is implemented as outlined in the
- \ October 1988 Communications of the ACM ( V 31 N 10 page 1192)
- 16807. 2CONSTANT A
- 2147483647. 2CONSTANT M
- 127773. 2CONSTANT Q \ M A D/
- 2836. 2CONSTANT R \ M A DMOD
- 2VARIABLE HI
- 2VARIABLE LO
- 2VARIABLE SEED
- 12345678. SEED 2!
-
- \ Returns a full cycle double random number
- : DRAND ( -- drand ) \ 0 <= drand < 4,294,967,295
- SEED 2@ Q UD/MOD HI 2! LO 2!
- LO 2@ A D* HI 2@ R D*
- 2OVER 2OVER D>
- IF D- SEED 2!
- ELSE D- M D+ SEED 2!
- THEN SEED 2@ ;
-
- \ Returns single random number less than n
- : LRAND ( n -- rnd ) \ 0 <= rnd < n
- DRAND DROP SWAP MOD ;
-
- \ From "Starting Forth" by Leo Brodie. page 265
- VARIABLE (RND) HERE (RND) ! \ This is the random number generator seed.
- : (RANDOM) ( -- n )
- (RND) @ 31421 * 6927 + DUP (RND) ! ;
- : BRAND ( u1 -- u2 ) \ 0 < u2 < u1
- (RANDOM) UM* SWAP DROP ;
-
- \ From "Mastering Forth" by Tracy and Anderson p 65.
- VARIABLE (SEED) 1234 (SEED) ! \ This is the random number generator seed.
- : RAND ( -- rnd)
- (SEED) @ 5421 * 1+ DUP (SEED) ! ;
- : TRAND ( u1 -- u2 ) \ 0 < u2 < u1
- RAND SWAP MOD ;
-
- \ Random Generator Selection with simple vectored execution.
- \ We will discuss vectored execution later in the Tutorial
- \ Usage:
- \ CHOOSE LRAND ( selects Lehmer's generator )
- \ CHOOSE BRAND ( selects Brodie's generator )
- \ CHOOSE TRAND ( selects Tracy's generator )
- VARIABLE CURRENT_GENERATOR \ VARIABLE FOR VECTORED EXECUTION
-
- : CHOOSE ( -- ) \ Use a new pseudo-random generator.
- ' CURRENT_GENERATOR ! ;
-
- : RANDOM ( n -- n? ) \ Invoke the pseudo-random generator.
- CURRENT_GENERATOR @ EXECUTE ;
-
- CHOOSE BRAND \ DEFAULT FUNCTION
- \ Set up Graphics + time to stablaize.
- : SCREEN-PREP ( -- ) HIGH 7 TENTHS ; \ For CGA
- \ : SCREEN-PREP ( -- ) VGA640 7 TENTHS ; \ For VGA
-
- \ 80X25 Color text mode.
- : C80 ( -- ) 3 MODE TEXT ;
-
- : RANDPLOT ( -- )
- SCREEN-PREP
- BEGIN
- 640 RANDOM \ A RANDOM X-VALUE
- 200 RANDOM \ A RANDOM Y-VALUE
- \ 480 RANDOM \ Replace above with this for VGA
- 1 DOT \ PLOT A POINT
- \ 15 DOT \ Replace above with this for VGA
- KEY? \ was a key pressed, if so pause
- IF KEY DROP KEY 13 = \ quit if cr is pressed.
- ELSE FALSE \ continue if any other key pressed.
- THEN
- UNTIL C80 ;
-
- ( Please move to Lesson 4 Part 14 )
-