Shop OBEX P1 Docs P2 Docs Learn Events
random number generator — Parallax Forums

random number generator

Mr. RichardMr. Richard Posts: 51
edited 2007-01-24 17:21 in BASIC Stamp
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
Josh

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-24 06:16
    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
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-01-24 17:21
    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
    main:
    DO WHILE IN1>0
    ··· seed=seed+1
    · IF seed >65535 THEN
    ··· seed=0
    · ENDIF
    LOOP
    RANDOM seed
    rnd=seed//5+1
    DEBUG DEC rnd," "
    time = rnd *1000
    PAUSE time
    GOTO main

    Jeff T.
Sign In or Register to comment.