PDA

View Full Version : Random Generator


TheTroll
06-24-03, 02:18 PM
I am trying to create a random generator to set some servos for a cat toy that I am making. I am new to Forth and I have no idea where to start. I have looked up some generators online but they all have library requirements. Does anyone have a random generator that works in Isomax or does anyone know what libraries that Isomax contains?

Thanks for any help that you can provide
Jim

rob
06-25-03, 12:13 AM
Here's a simple one based on a C library routine but all done in Forth. The range of values is from 0 to RAND_MAX and you can start the sequence off in different spots by using a different seed value:

HEX
( ==== Simple pseudo random number generator ==== )
VARIABLE seed
7FFF CONSTANT RAND_MAX ( maximum random number )

: RANDOM ( -- n ) ( return a pseudo random number )
seed @ 4E6D * 3039 + RAND_MAX AND DUP seed ! ;

: SRAND ( n -- ) seed ! ;

Rob