Shop OBEX P1 Docs P2 Docs Learn Events
Random time within limits, long pauses — Parallax Forums

Random time within limits, long pauses

gchrtgchrt Posts: 33
edited 2005-06-09 22:16 in BASIC Stamp
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

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-06-09 07:34
    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 WilliamsJon Williams Posts: 6,491
    edited 2005-06-09 11:14
    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 WalkerTom Walker Posts: 509
    edited 2005-06-09 13:04
    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 WilliamsJon Williams Posts: 6,491
    edited 2005-06-09 13:41
    I updated the code, just to prevent any confusion.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • gchrtgchrt Posts: 33
    edited 2005-06-09 15:13
    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 WilliamsJon Williams Posts: 6,491
    edited 2005-06-09 17:01
    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
  • gchrtgchrt Posts: 33
    edited 2005-06-09 17:53
    Ok, so I see.

    Any ideas about an internal random trigger on a project that people don't really interact with?
  • Tom WalkerTom Walker Posts: 509
    edited 2005-06-09 17:53
    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 WilliamsJon Williams Posts: 6,491
    edited 2005-06-09 18:00
    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
  • gchrtgchrt Posts: 33
    edited 2005-06-09 18:12
    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 AllenTracy Allen Posts: 6,658
    edited 2005-06-09 19:49
    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
  • Vern GranerVern Graner Posts: 337
    edited 2005-06-09 22:16
    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. 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
    
    
Sign In or Register to comment.