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

Random Numbers

Daniel_LangkildeDaniel_Langkilde Posts: 2
edited 2006-02-01 14:28 in BASIC Stamp
Hi!

I am trying to use the RANDOM function in PBASIC 2,5 to create series of random numbers. But since the code is psuedorandom i need seed values. In the examples included in the helpfile the initiate the random sequens with the same value over and over again. I want something that changes so I don't get the same values over and over again!

Thnx!

Comments

  • Steve JoblinSteve Joblin Posts: 784
    edited 2006-02-01 13:32
    Can you add a button to your project? If so, you can run a loop of generating random numbers until the button is pressed... your ability to press the button exactly the same microsecond from starting the program is near impossible, so you will always get a different randon number...

    Step 1: Generate Random Number
    Step 2: If button pressed then goto "Step 3", otherwise, goto Step 1
    Step 3: Continue with the execution of the rest of your program
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-01 14:22
    Here's how you can implement Steve's suggestion:

    Main:
    · DO
    ··· RANDOM lottery
    · LOOP UNTIL (StartBtn = Pressed)

    We often use this kind of structure to create a random timing delay for a triggered prop.· To create a delay between X and Y, you can do this:

    · timer = lottery // (Y - X + 1) + X

    ... so it you wanted to create a delay between one and five seconds, the actual code would be:

    · timer = lottery // 4001 + 1000
    · PAUSE timer

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • John R.John R. Posts: 1,376
    edited 2006-02-01 14:28
    If you have a RTC (Real Time Clock) available, you can also use a segment of the current time (e.g. the seconds value) as the seed.

    Another possibility might be to base the seed off states of various input pins used for other purposes, if these would be in different and somewhat random states when initializing the random number generator.

    This problem of the seed, and the "unrandom" sequence of "random" numbers is not unique to the STAMP.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.

    8 + 8 = 10
Sign In or Register to comment.