Shop OBEX P1 Docs P2 Docs Learn Events
Random numbers — Parallax Forums

Random numbers

Mongojerry_2Mongojerry_2 Posts: 15
edited 2005-03-13 02:11 in BASIC Stamp
Is it possible to generate a random number within limited parameters, such as a number from 1 to 20? Or 20-40? Whatever?
Is it possible to have the uC pause, or better yet, sleep, for 30 minutes? Seems to me if the cmd has to be in ms I would have to tell it to Sleep 60000 to get 1 minute of sleep, or Pause 60000. Finally, combining the 2 concepts, can I get it to pause a random amount of time from 20-30 minutes? Could I quantize it so that it's either 21, 22, 23. . . 30 minutes but not anything in between?

Here's my proposed solution to the puzzle, but I'm betting both that this wouldn't work and that even if it would there's a much easier way. I only found the MIN and MAX commands after I did this--would they help? Could I use them to directly generate a random number from 20 to 30? Lastly, how can I get it to sleep for 20-30 minutes instead of 20-30 milliseconds?





Post Edited (Mongojerry_2) : 3/11/2005 8:15:42 AM GMT

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-11 14:07
    On RANDOM...

    Yes, you can create a limited range. Remember that the RANDOM function spits out values from a 65536-element (0 - 65535) list, so dividing the output into your range is an easy way to go. Let's use your 20 - 30 suggestion:

    · RANDOM randVal
    · delay = randVal / 6553 + 20

    For longer sleep periods you'll need to use a loop.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Mongojerry_2Mongojerry_2 Posts: 15
    edited 2005-03-11 17:16
    Jon Williams said...
    On RANDOM...

    Yes, you can create a limited range. Remember that the RANDOM function spits out values from a 65536-element (0 - 65535) list, so dividing the output into your range is an easy way to go. Let's use your 20 - 30 suggestion:

    · RANDOM randVal
    · delay = randVal / 6553 + 20

    For longer sleep periods you'll need to use a loop.

    That is downright elegant. Thanks!

    As to sleeping for 20 minutes I need it to sleep for 65,535 milliseconds about 20 times. Seems to me for what I want to do, building on your code above:

    For X =1 to delay
    · Sleep 65535
    Next

    Does delay have to be an integer for this to work?
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-11 17:20
    In your program delay can have a value up to 30 so you'll need to define it as a byte. Using a Word would just be a waste of variable space.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Mongojerry_2Mongojerry_2 Posts: 15
    edited 2005-03-11 17:21
    Jon Williams said...
    In your program delay can have a value up to 30 so you'll need to define it as a byte. Using a Word would just be a waste of variable space.

    Right--I'll make that change later as I work in your solution (which is obviously vastly simpler than my own). I wonder though--does my program look like it actually would have worked?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-11 18:27
    I think your sleep value is out of whack -- you want to sleep for 'delay' minutes, right?· I think this is what you want:

    Go_To_Sleep:
    · FOR restTime = 1 TO delay
    ··· SLEEP 60
    · NEXT
    · RETURN

    The units for sleep are in seconds, so SLEEP 60 will go into low-power mode for approximately one minute (see SLEEP in the help file for details).·

    Another thing to keep in mind is that the RANDOM function is actually pseudo-random, so to get real randomness you'll want to tumble the seed until you actually need the random value.· You can do this in your main loop:

    Main:
    · ' main loop code
    · IF (Some_Condition) THEN
    ··· GOSUB Go_To_Sleep
    · ENDIF
    · RANDOM delay
    · GOTO Main

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Mongojerry_2Mongojerry_2 Posts: 15
    edited 2005-03-11 18:47
    ·
    ·
    ·

    I·see-- I thought sleep was in milliseconds.

    I understand about tumbling the random number, but my interest in this program is for it to go to sleep for 20-30 minutes when the program starts. My solution was to tumble the seed value one time:

    Pause_routine:
    ··RandVal·= 11000
    ··For X = 1 to 2
    ··· ·Random RandVal
    ··Next
    · Delay = RandVal / 6553 + 20
    · For X = 1 to Delay
    ··· Sleep 60
    · Next


    Will that work?

    Post Edited (Mongojerry_2) : 3/11/2005 6:58:39 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-11 23:19
    It won't be random -- every time your run the program you will sleep for the same amount of time; that's the nature of the RANDOM fuction. You have to introduce some external randomness to get it to be truly random.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Mongojerry_2Mongojerry_2 Posts: 15
    edited 2005-03-13 00:37
    I don't understand why the program as I've now got it written below will produce a result that the sleep time will be the same every time it runs. Could you help me understand that?

    InitialRandom_Routine: 'To get the first random #
    RandVal = 11000
    FOR X = 1 TO 5
    RANDOM RandVal
    NEXT
    Waiting_routine: 'To sleep 20-30mins
    RANDOM RandVal
    Duration = RandVal / 6553 + 20 '20-30
    FOR X = 1 TO Duration
    SLEEP 60
    NEXT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-13 00:44
    Okay, I'll try again.

    RANDOM produces a list of random-looking list values that don't repeat until after 65536 iterations of the function. As the function is mathmatically based, the same seed (11000 in your program) will ALWAYS produce the same result. The only way to get true randomness is to insert some external event -- like a human pressing a button, etc.

    You could, I suppose, use a really low-quality capacitor and read it with the POT command; the low quality cap *might* give you a different value when you read it. If you use a photocell for the R component of the POT circuit, the light falling on it when read will affect the reading.

    On RANDOM not being truely random ... my boss (the guy who invented the BASIC Stamp) wrote a really clever Simon game that takes advantage of RANDOM's behavior

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Mongojerry_2Mongojerry_2 Posts: 15
    edited 2005-03-13 00:49
    I'm glad you're here--I'm really puzzling over this and I appreciate your help. You wrote: "As the function is mathmatically based, the same seed (11000 in your program) will ALWAYS produce the same result." You're right, and I understand that. That's why after the Result = 11000 line I put it through a loop of generating new random numbers with the seed value being the previous random result. Doesen't that get me to random. I know if all I had was a constant seed value of 11000 I'd have the same result every tiume, but the program I posted in my last post has more:

    RandVal = 11000
    FOR X = 1 TO 5
    RANDOM RandVal
    NEXT
    That uses random seed values. By the time this goes to the next line, RandVal is no longer based on the 11000 seed value. That's where I'm confused.
  • Mongojerry_2Mongojerry_2 Posts: 15
    edited 2005-03-13 00:58
    Oh! I think I see. If the first random value isn't really random then the subsequent ones in the following loop are also predictable. I guess I need a button to be pressed by the user at the start of the program so that the button will be the trigger to stop the tumbling of the random # generator--since the button will be pressed at different times, it would be truly random. Thanks for helping me understand this.
  • Mongojerry_2Mongojerry_2 Posts: 15
    edited 2005-03-13 01:42
    I'm back--I'm trying to program it to keep tumbling the random numbers until a button is pressed--that should give me the external randomness you showed me I need, I think. I'm not totally clear on the button syntax, but here's how I've got it right now. If it does what I have in mind it will tumble the random numbers until the button is pressed and then jump to Waiting_Routine.

    Assuming this works I have another question. Should I want to use the button for another purpose later in the program do I have to clear its workspace to zero before doing that?

    InitialRandom_Routine:················· 'To get the first random #
    ··· RandVal = 11000
    ··· DO
    ····· RANDOM RandVal
    ····· BUTTON 0, 1, 255, 255, Btn, 1, Waiting_Routine
    ··· LOOP
    Waiting_routine:······················· 'To sleep 20-30mins
    · RANDOM RandVal
    · Duration = RandVal / 6553 + 20
    · FOR X = 1 TO Duration
    ··· SLEEP 60
    · NEXT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-13 02:11
    You don't need to use BUTTON, simply monitor an input pin for a change of state:

    Reset:
    · DO
    ··· RANDOM delay
    ··LOOP UNTIL (StartBtn = Pressed)

    Use the PIN definition for StartBtn and define a CONstant for Pressed (1 for active-high [noparse][[/noparse]pin pulled down], 0 for active-low [noparse][[/noparse]pin pulled up])


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
Sign In or Register to comment.