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.
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.")
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.