Random time within limits, long pauses
gchrt
Posts: 33
Hi,
What is the procedure to generated a random number between 3 and 10?
I see Random produces a number between 0 and 65535.
Second - what is the common means to pause for longer than 65535 milliseconds?
thanks
What is the procedure to generated a random number between 3 and 10?
I see Random produces a number between 0 and 65535.
Second - what is the common means to pause for longer than 65535 milliseconds?
thanks
Comments
Just generate a random number from 0 to 7 and then add 3 to the result. Look at the modulus ( // ) operator for hints at limiting RANDOM.
Multiple PAUSE statement will give you pauses greater than 65535 milliseconds. Alternatively, you might use a loop to execute the same PAUSE statement more than once.
Regards,
Bruce Bates
Trigger··· PIN··· 15············ ' active-high trigger
rndVal···· VAR····Word·········· ' for random
secs······ VAR··· Nib··········· ' seconds, 0 - 15
Main:
· DO
··· RANDOM rndVal··············· ' tumble random value
· LOOP UNTIL (Trigger = 1)······ ' wait for trigger input
· secs = rndVal // 8 + 3·········' create 3 to 10 second delay
· GOSUB Wait_1Sec··············· ' run the delay
· GOTO Main
Wait_1Sec:
· DO WHILE (secs > 0)············' timer expired?·
··· PAUSE·1000·················· ' no, wait 1 second
··· secs = secs - 1············· ' decrement timer
· LOOP
· RETURN·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 6/9/2005 1:41:38 PM GMT
Please excuse me if I missed something...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Since, in the above code, randVal will change on each go round as the seed will that not give me enough change to produce my pseudo random result since I don't particularily have an external event trigger at the moment?
Subs can be called from subs correct?
rndVal· · VAR··· Word
idx······ VAR··· Nib
secs····· VAR··· Nib
Main:
· FOR idx = 1 TO 5
··· RANDOM rndVal
··· secs = rndVal // 8 + 3
··· DEBUG DEC secs, CR
· NEXT
· END
Notice how when you run the program it prints:
3
3
5
7
8
... every time the program is run?· This is the nature of psuedo-random.· By adding an external event you introduce some true randomness.
Yes, subroutines can be called from other subroutines -- this fact makes them very useful.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Any ideas about an internal random trigger on a project that people don't really interact with?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Now I'm just groping here.
Could a capacitor discharge be a trigger and would its discharge decay be random enough?
Again I'm just thinking here and have no real idea how to implement this.
You get the best random numbers if you wire it poorly. That is, use long lengths of wire looped around the power supply or cpu. You can use the value of X as a seed for the RANDOM function.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Vern
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔