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

Random numbers...

pizzaguypizzaguy Posts: 4
edited 2009-02-06 23:58 in BASIC Stamp
Im trying to make a program to generate a random number between 1 and 10.

I figured I would start by doing this:

RANDOM startnum 
  number = startnum / 6553.5



if i understand RANDOM, then this should make a decimal between 1 and 10, but i need it to be rounded to whole numbers. How would i go about doing this?

Comments

  • ProcessingData...ProcessingData... Posts: 208
    edited 2009-02-06 18:03
    Pizzaguy, try something like this:

    startnum·VAR Nib

    number·· VAR· Nib



    DO

    RANDOM startnum

    LOOP UNTIL startnum < 11

    number = startnum


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Basic Stamp,···· Propeller,·· · SX,·· FUN!


    START:·
    >Proccessing Data. . . .··
    >Task Complete. . .·.
    >Saving Data. . . .
    >Entering SLEEP Mode. . . .
    >Signing OFF




    Post Edited (ProcessingData...) : 2/6/2009 6:11:19 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-06 18:18
    Another good way to do this is

    RANDOM startnum
    result = (startnum // 10) + 1
  • pizzaguypizzaguy Posts: 4
    edited 2009-02-06 22:52
    Thanks mike that worked great... but when I set up a script to show all of the numbers generated via DEBUG, the numbers are always the same. Is there a way around that?
    I found the RANDOM example where it continuously generates a number to make a number that "appears" random. But im not quite sure how to integrate it into my program.

    But basically my main question now is, is there a way to get the Time, or some sort of variable that will be based on the time, since time will never be the same making it a good seed for RANDOM?

    Post Edited (pizzaguy) : 2/6/2009 11:06:47 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-06 23:03
    1) I'd have to see the rest of your program to see why you're not getting random numbers. I can't tell from just your description.

    2) I'd have to see the example you found. I can't guess what it is.

    3) The Stamps do not have a built-in real time clock, so you can't get the time from somewhere in the chip. You can add an external real time clock chip like the DS1302 or DS1307 and there are examples of this in the Nuts and Volts Columns collection. If you had any reason to use an external keypad or some external pushbutton, you could use the time between button pushes as a "random" input.
  • pizzaguypizzaguy Posts: 4
    edited 2009-02-06 23:10
    This is my basic code at the moment (its just a modification of some demo code for the Parallax SumoBot)


    ' -----[noparse][[/noparse] Title ]----------------------------------------------------------------
    ' Mini Sumo 3.3 : Simple Mini Sumo (blind)
    ' {$STAMP BS2}
    
    ' -----[noparse][[/noparse] I/O Definitions ]------------------------------------------------------
    
    LMotor          CON     13                      ' left servo motor
    RMotor          CON     12                      ' right servo motor
    
    
    ' -----[noparse][[/noparse] Constants ]------------------------------------------------------------
    
    LFwdFast        CON     1000                    ' left motor forward; fast
    LFwdSlow        CON      800                    ' left motor forward; slow
    LStop           CON      750                    ' left motor stop
    
    RFwdFast        CON      500                    ' right motor forward; fast
    RFwdSlow        CON      700                    ' right motor forward; slow
    RStop           CON      750                    ' right motor stop
    
    ClrEOL          CON     11                      ' clear to end of line (DEBUG)
    
    ' -----[noparse][[/noparse] Variables ]------------------------------------------------------------
    
    pulses          VAR     Byte                    ' counter for motor control
    temp            VAR     Byte
    
    ' -----[noparse][[/noparse] EEPROM Data ]----------------------------------------------------------
    
    RunStatus       DATA    $00                     ' run status
    
    ' -----[noparse][[/noparse] Initialization ]-------------------------------------------------------
    
    Reset:                                      ' user Reset button as On-Off
      READ RunStatus, temp                          ' read current status
      temp = ~temp                                  ' invert status
      WRITE RunStatus, temp                         ' save status for next reset
      IF (temp > 0) THEN Start_Delay                ' run now?
      END
    
    Start_Delay:                                    ' mandatory five second delay
      PAUSE 10
    
    ' -----[noparse][[/noparse] Main Code ]------------------------------------------------------------
    
    Main:
    Go_Fwd:
      PULSOUT LMotor, LFwdFast
      PULSOUT RMotor, RFwdFast
      GOTO Main
    



    what i want to do is have the robot go strait for a random distance.

    This is the RANDOM example: (according to the PBASIC reference "RANDOM is executed continuously (using the previous resulting number as the next seed value) while the program waits for the user to press a button.")

    ' RANDOM.BS2
    ' Connect a button to I/O pin 7 as shown in the figure in the RANDOM
    ' command description and run this program. This program uses RANDOM to
    ' simulate a coin toss. After 100 trials, it reports the total number of
    ' heads and tails thrown.
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    Btn             PIN     7                       ' button input
    
    flip            VAR     Word                    ' a random number
    coin            VAR     flip.BIT0               ' Bit0 of the random number
    trials          VAR     Byte                    ' number of flips
    heads           VAR     Byte                    ' throws that come up heads
    tails           VAR     Byte                    ' throws that come up tails
    btnWrk          VAR     Byte                    ' workspace for BUTTON
    
    
    Start:
      DEBUG CLS, "Press button to start"
    
    Main:
      FOR trials = 1 TO 100                         ' flip coin 100 times
    
    Hold:
        RANDOM flip                                 ' randomize while waiting
        BUTTON Btin, 0, 250, 100, btnWrk, 0, Hold   ' wait for button press
        IF (coin = 0) THEN                          ' 0 = heads, 1 = tails
          DEBUG CR, "Heads!"
          heads = heads + 1                         ' increment heads counter
        ELSE
          DEBUG CR, "Tails..."
          tails = tails + 1                         ' increment tails counter
        ENDIF
      NEXT
    
    Done:
      DEBUG CR, CR, "Heads: ", DEC heads, " Tails: ", DEC tails
      END
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-06 23:58
    ' -----[noparse][[/noparse] Variables ]------------------------------------------------------------
    
    pulses          VAR     Byte                    ' counter for motor control
    temp            VAR     Byte
    temp2          VAR     Byte
    ranVal          VAR     Word
    
    ' -----[noparse][[/noparse] EEPROM Data ]----------------------------------------------------------
    
    RunStatus       DATA    $00                     ' run status
    RanNum         DATA     $00,$00              ' random value (a word)
    
    ' -----[noparse][[/noparse] Initialization ]-------------------------------------------------------
    
    Reset:                                      ' user Reset button as On-Off
      READ RunStatus, temp                          ' read current status
      temp = ~temp                                  ' invert status
      WRITE RunStatus, temp                         ' save status for next reset
      READ  RanNum, temp2
      READ RanNum+1,ranVal         ' Get random value
      ranVal = ranVal * 256 + temp2
      RANDOM ranVal
      WRITE RanNum,ranVal           ' Store updated random value
      WRITE RanNum+1,ranVal/256
      IF (temp > 0) THEN Start_Delay                ' run now?
      END
    
    


    This will ensure that "ranVal" will have a new random value every time the Stamp is reset until the program is downloaded the next time which initializes the random value to zero. The robot will move every other time the Stamp is reset whether it's powered on or the reset button is pressed. The Stamp can't tell the difference between a reset from a power on or from a reset button push.

    You can use "ranVal" any way you want to in the rest of the program.
Sign In or Register to comment.