Table of Contents

Advertisement

5: BASIC Stamp Command Reference – RANDOM

RANDOM

2
2
2
RANDOM Variable
1
2
e
sx
p
Function
Generate a pseudo-random number.
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. The following example demonstrates this:
1
SYMBOL
Loop:
Result = 11000
RANDOM Result
DEBUG Result
GOTO Loop
-- or --
2
2
2
2
Result
e
p
sx
Loop:
Result = 11000
RANDOM Result
DEBUG DEC ? Result
GOTO Loop
In this example, the same number would appear on the screen over and
over again. This is because the same seed value was used each time;
specifically, the first line of the loop sets Result to 11,000. The RANDOM
command really needs a different seed value each time.
"Result =" line out of the loop will solve this problem, as in:
BS1
BS2
• 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.
Result = W0
VAR
WORD
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 251
BS2e BS2sx BS2p
' Set initial "seed" value
' Generate random number.
' Show the result on screen.
' Set initial "seed" value
' Generate random number
' Show the result on screen.
Moving the

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?

Table of Contents