Random numbers...
Im trying to make a program to generate a random number between 1 and 10.
I figured I would start by doing this:
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?
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
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
RANDOM startnum
result = (startnum // 10) + 1
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
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.
' -----[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 Mainwhat 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 ENDThis 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.