PDA

View Full Version : Random time within limits, long pauses



gchrt
06-09-2005, 03:12 PM
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

Bruce Bates
06-09-2005, 03:34 PM
gcrt -

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

Jon Williams
06-09-2005, 07:14 PM
Keep in mind that RANDOM is actually pseudo-random, so if you can tumble the generator until some external event (which happens randomly), you'll get more random results.· Here's a bit of code that implements Bruce's suggestions:


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

Tom Walker
06-09-2005, 09:04 PM
Also be aware that Jon's code assumes you have defined rndVal (WORD), Trigger (BIT, PIN, BYTE, WORD), and secs (BYTE, WORD) elsewhere.

Please excuse me if I missed something...

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...

Jon Williams
06-09-2005, 09:41 PM
I updated the code, just to prevent any confusion.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

gchrt
06-09-2005, 11:13 PM
Thanks for the help, it is just what I needed. I knew there was something about dividing by the number limit but just could not remember.

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?

Jon Williams
06-10-2005, 01:01 AM
You can of course use RANDOM without the external trigger, but as the little test program below demonstrates, you'll always get the same sequence of delays:

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

gchrt
06-10-2005, 01:53 AM
Ok, so I see.

Any ideas about an internal random trigger on a project that people don't really interact with?

Tom Walker
06-10-2005, 01:53 AM
Just be aware that there is a limit to how many "levels" of GOSUB you may crawl in to (and reliably crawl out of). The PBASIC Help file is your friend...

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...

Jon Williams
06-10-2005, 02:00 AM
It was once suggested some time ago·that you could look at a floating pin as a trigger, or floating set of inputs to use as a seed, but I find this unreliable.· You could connect a noise generator to the Stamp and use that input


gchrt said...
Ok, so I see.

Any ideas about an internal random trigger on a project that people don't really interact with?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

gchrt
06-10-2005, 02:12 AM
Could it be a silent noise generator?

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.

Tracy Allen
06-10-2005, 03:49 AM
The last digits of an RCTIME command can be a good randomizer. Your capacitor discharge idea will work. Connect a 0.01 uF capacitor in parallel with a 1 Mohm or 2.2 Mohm resistor from a Stamp pin to Vss. Here is a snippet program that gives an illustration of how random the least significant digits can be:


'{PBASIC 2.5}
x VAR Word
DO
HIGH p15
RCTIME 15,1,x
DEBUG REP "*" \ x//70, CR ' bar graph of random length
LOOP



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 (http://www.emesystems.com)

Vern Graner
06-10-2005, 06:16 AM
I was able to create a "random" seed when I developed a program on a Basic Stamp Activity Board board. The BSAC had a built in Pot/Cap circuit to allow for demonstrations of RCTIME. I found that when your read the value with RCTIME, the value tended to "jitter" around a couple of values, thereby provising a "random" seed value. Also I could move the POT around a bit during or before the stamp was powered up to "change up" the seed. http://forums.parallax.com/images/smilies/smile.gif

Vern

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔



Vern Graner CNE/CNA/SSE | "If the network is down, then you're
Senior Systems Engineer | obviously incompetent so why are we
Texas Information Services | paying you? Of course,if the network
http://www.txis.com | is up, then we obviously don't need
Austin Office 512 328-8947 | you, so why are we paying you?" ©VLG