My teacher gave us an assignment I need some help.

Okay, so we made a random number generator, but we keep getting the same numbers every time we run the program. We know that to change the numbers you need to change the seed value, but our teacher won't let us change it every time we run the program. Is there a way to make the program automatically say add one to the seed every time? Thanks any help is appreciated.


  • Tracy AllenTracy Allen Posts: 6,404
    edited 2018-01-19 - 20:15:41
    You can ask your user to press a button as the program starts, and use something like the time they hold the button down as your seed.

    An R and a C on an input pin can be made to give an uncertain number with the RCtime command, especially if you make poor wiring, that is, long and loopy, or use a photosensitive device in place of the R.
  • Page 360 of the Basic Stamp manual has the procedure that Tracy Allen put forward.
    Any com port in a storm.
    Floating point numbers will be our downfall; count on it.
    Imagine a world without hypothetical situations.
  • Okay thanks guys ill have to look at that page
  • Beau SchwabeBeau Schwabe Posts: 6,416
    edited 2018-01-20 - 16:30:20
    Another option is to update the seed by incrementing it or generating a random value for it every 500 or 1000 or whatever number of program iterations and "storing" the new seed value so that upon startup the saved seed value is restored and continues. One thing to keep in mind is the number of EEPROM writes that can be written. Some EEPROMS have a limit, although in my 35 years of programming, testing, etc. I have never worn an EEPROM out by excessively writing to it.

    Beau Schwabe --- Robotics applications- PCB design, embedded software, and mechanical
    Oklahoma Robotics - - ෴෴ - ෴෴

  • StandOffCape,

    How are you generating your random number and does your program run once or keep repeating?

    It is best if you can post your program because maybe there is an error in it.
  • StandoffCapeStandoffCape Posts: 4
    edited 2018-01-26 - 20:31:59
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    result1   VAR  Word
    final1    VAR  Byte
    result2   VAR  Word
    final2    VAR  Byte
    counter   VAR   Byte
    PulseCount  VAR Word
    result1 = 10 + counter = 1 TO 5
    FOR counter = 1 TO 1
    final1 =((result1//4) +1)
    RANDOM result1
     DEBUG DEC ? final1
    PAUSE 50
    result2 = 10
    FOR counter = 1 TO 1
    final2 =((result2//4) +4)
    RANDOM result2
     DEBUG DEC ? final2
    PAUSE 50
    GOTO IF_Then
    IF (final1 = 3) AND (final2 = 6) THEN
      FOR counter = 1 TO 23
        PULSOUT 13, 850
        PULSOUT 12, 850
      PAUSE 20
      FOR counter = 1 TO 72
        PULSOUT 13, 850
        PULSOUT 12, 650
      PAUSE 20

    Here is my code.
  • Also how do I use the time as the seed?
  • You can use COUNT to measure the time the button is pressed and store that as your seed variable.
    Any com port in a storm.
    Floating point numbers will be our downfall; count on it.
    Imagine a world without hypothetical situations.
  • Tracy AllenTracy Allen Posts: 6,404
    edited 2018-01-22 - 21:24:31
    If you have a button connected so that it brings a pin low when it is pressed:
    DEBUG "please press the button to start",13
    LOOP UNTIL myButtonPin=0
      RCTIME myButtonPin, 0, PulseCount
    LOOP UNTIL myButtonPin=1 .   ' loops at 131ms intervals until the pin goes high

    The idea is that the Stamp waits as long as necessary in the DO loops, until the user has pressed and then released the button. PulseCount will be the last residual value from RCTIME, and that can be the seed.

    If the device is connected to a computer, you could ask the user to press a random key on the keyboard, and use that as a seed.
    DEBUG "please press random key to start",13
    DEBUGIN seed

  • The RCTIME command is explained on page 363 of the Stamp manual.

    You could use one of the circuits and code shown on page 364, with a temperature sensor (thermistor) or light sensor (LED connected backwards), to give an environmentally determined seed.
  • StandoffCape,

    I take it that result1 and result2 will be Random numbers.
    Is there a particular range of values they need to be in?

    Also, why do you have 2 FOR...NEXT loops that go from 1 to 1 since nothing will change?

    Is this code being run on a Boe-Bot because I see you are sending what look to be servo pulses using 2 FOR...NEXT loops.
    Also, I am guessing that you want one set to run when the 2 magic numbers come up and then other set to run when they don't, so put an ELSE before the 2nd FOR...NEXT loop.

    The GOTO If_Then does nothing since If_Then follows that line and it's good practice to avoid using GOTO.

    In addition to the BASIC Stamp Manual, you should also look at the What's a Microcontroller and StampWorks texts.
Sign In or Register to comment.