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.
In applications requiring more apparent randomness, it's necessary to
"seed" RANDOM with a more random value every time. For instance, in
the demo program below, 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. Since the user can’t control the timing
of button presses very accurately, the results approach true randomness.
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
PARALLAX
"BASIC Stamp Syntax and Reference Manual Version 2.1
Bookmarks