Randon number at start up
usabob01
Posts: 15
I
I have been trying to find a way to get a different random number (1 thru 10) every time I start the program. It seems all code I find always starts off with the same number.I want a different number every time I start. Is this possible . Thanks for all Help.
What can I add to this to make it choose from 1 thru 10 rather than hundred's
timeCounter Var Word
do
Loop UNTIL IN12 =1
FOR timeCounter = 1 to10
DO
PAUSE 1
timeCounter = timeCounter +1
Loop UNTIL IN12 =0
DEBUG DEC Time Counter,CR
Sorry I don't now how to paste in this box yet
I have been trying to find a way to get a different random number (1 thru 10) every time I start the program. It seems all code I find always starts off with the same number.I want a different number every time I start. Is this possible . Thanks for all Help.
What can I add to this to make it choose from 1 thru 10 rather than hundred's
timeCounter Var Word
do
Loop UNTIL IN12 =1
FOR timeCounter = 1 to10
DO
PAUSE 1
timeCounter = timeCounter +1
Loop UNTIL IN12 =0
DEBUG DEC Time Counter,CR
Sorry I don't now how to paste in this box yet
Comments
Or you could require a button press to start your program. Measure the duration of the button press and use that as a seed number.
-Phil
-Phil
Best Day Ever!
See the Syntax manual for more info.
He wants a random seed, so the program behaves differently every time it's run.
-Phil
...got it.
Also, see the Demo Program on page 177 of the BASIC Stamp Manual V2.2 for more additional information.
It works by first detecting the selected pin's (15 in this case) natural input state, then using OUT to flip that state and RCTIME to measure how long it takes to flip back. In order for this to work you have to touch the affected pin (or use a 100+ meg resistor to Vdd or Vss). The least-significant bit of the time it takes will be random. To remove any bias, the program samples two bits at a time. If they're equal, it samples two more until they are unequal. It then shifts the first bit into the result. This process repeats eight times for eight bits.
-Phil
Function: Generate a pseudo-random number.
Variable is a variable (usually a word) whose bits will be scrambled to produce a random number. Variable acts as RANDOM's input and its result output. Each pass through RANDOM stores the next number, in the pseudorandom sequence, in Variable.
Explanation:
RANDOM generates pseudo-random numbers ranging from 0 to 65535. They’re called “pseudo-random” because they appear random, but are generated by a logic operation that uses the initial value in Variable to "tap" into a sequence of 65535 essentially random numbers. If the same initial value, called the "seed", is always used, then the same sequence of numbers is generated.
via BASIC Stamp Manual V2.2