Random Number
Special_K
Posts: 162
Hello all,
I am working on a·bit of code·that will provide a random number. Let’s say from one to 3 and given the output number the Boe-Bot will use a different movement subroutine.
My code is this
··
RANDOM randnum················· ' random value and place in randnum
randnum=randnum //·3··········· 'Limit the result to 0-3·I think
IF randnum < 1 THEN
GOSUB Slow_Turn_Right
ELSEIF randnum > 2 THEN
GOSUB Slow_Turn_Left
ENDIF
The Boe0Bot backs up and turns left every time. Any ideas?
I am working on a·bit of code·that will provide a random number. Let’s say from one to 3 and given the output number the Boe-Bot will use a different movement subroutine.
My code is this
··
RANDOM randnum················· ' random value and place in randnum
randnum=randnum //·3··········· 'Limit the result to 0-3·I think
IF randnum < 1 THEN
GOSUB Slow_Turn_Right
ELSEIF randnum > 2 THEN
GOSUB Slow_Turn_Left
ENDIF
The Boe0Bot backs up and turns left every time. Any ideas?
Comments
You might want to update your program such that as the robot·as moving forward it tumbles the RANDOM function.· Then, when an obstacle is detected, the new value is used.· The key to getting a truly random value from RANDOM is constantly calling it until some (random) external event occurs.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Here is the entire program code.
Where would I place the random number tumbler?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
thanks again.
I´m all new to BASIC,
I looked at the code and I can´t figure how to change it so the RANDOM function works...
pleas help me and make it clear, it´s so frustrating.
Thanks
I feel so dumb not figuring out how to change the code so the RANDOM function will work.
I have tryed moving the RANDOM function lines... but no luck.
Please help me and explain!
Thanks!
-Phil
The trick is to use a "random" number for the seed by using some external event such as user pressing and letting go of a button.
if you Seed +=1 while button is pressed it would be hard for the user to always letting go of the button the same time all the time.
Use a new variable to get the result in-bounds.
This way seed continues to be unique (within 65536 values) while randnum is restricted to 0-2.
What you had before kept resetting the "seed" to 0, 1, or 2 and severely limited the randomness.
Following on what tonyp12 said, you need to initialize "seed" with something at power-up. Otherwise it always starts at zero and will always produce the same pseudorandom number sequence. You could do that by timing a button press or running RCTIME on a photocell. Anything that produces different results each time.
A typical solution in computer science is to use time. By this I mean most computers have a clock and it started counting since some epoch date. If you start your program running and you are at the line where you are looking for a seed value, you can grab some part of time (say the current milliseconds, or you can do some math on the total milliseconds since the epoch, etc) and that becomes your seed value. So what does the clock represent? It represents an external input to an otherwise deterministic system. By deterministic I mean, if you start a program running and the program has no inputs to change its course of execution, it will ALWAYS do the same thing. So the really fun part about the random seed problem is that you can get quite creative with what you use for input to create a seed value. Suppose you have a switch which is "noisy"... it "bounces" when you flip the switch. You might creatively use that noise from the switch to generate a random value. The possibilities are endless but the theory is the same, you need an external input that is not predictable. With the time example, it is not predictable when someone is going to run your program to the thousandth of a second and therefore the value of the clock is anyone's guess at the point when your poll for your seed value from the clock.
I use the DEBUG function to show how the RANDOM function with my modifyed
seed VAR Word RANDOM seed randnum = seed // 3
works.
Now it gives me nice numbers=)
Thanks!