View Full Version : random number generator

Mr. Richard
01-24-2007, 08:52 AM
I am trying to add a random timing event to a code for a BS2. I am sure that this is well documented, but I could not find it.

Is there a way to create a random number for a counter? I would like to randomize a pause before an event (pause 1-5 seconds then high X) How do I use a random number in a loop?

Thanks for the help

Magic Smoke Theory of Electronics –
Inside every electronic part there is magic smoke.
The magic smoke is what makes everything work.
If you release the magic smoke, the part stops working!

Chris Savage
01-24-2007, 01:16 PM
Mr. Richard,

Please see the RANDOM command in the Help File or the BASIC Stamp manual. The RANDOM command randomizes a variable. The trick is to make sure the command is within the loop and the results will be better if the loop runs an arbitrary number of iterations, such as until a button is pressed or some other event. I hope this helps. Take care.

Chris Savage
Parallax Tech Support

01-25-2007, 12:21 AM
Hi Josh, the random command generates a series of numbers dependent on the initial seed value, this routine takes a number from within the do loop (waiting for a button press) and sets the seed value,the seed is randomized and·then its trimmed to between 1 and 5 using the mod operator,·last its·multiplied by 1000 to give a time between 1 and 5 seconds.

seed VAR Word
rnd VAR Word
time VAR Word
··· seed=seed+1
· IF seed >65535 THEN
··· seed=0
DEBUG DEC rnd," "
time = rnd *1000
PAUSE time
GOTO main

Jeff T.