Propeller Manual

Table Of Contents
2: Spin Language Reference – Operators
Here, the value of X is shifted right 2 bits, maintaining the sign, and the result is stored back
in
X. The assignment form of Shift Arithmetic Right may also be used within expressions for
intermediate results; see Intermediate Assignments, page 147.
Random ‘
?
The Random operator is a special, immediate operator that uses a variable’s value as a seed to
create a pseudo random number and assigns that number to the same variable. It can only be
used in run-time variable expressions. Random has two forms, forward and reverse,
depending on which side of the variable it appears on. The forward form appears to the left
of a variable and the reverse form appears to the right of a variable.
Random generates pseudo-random numbers ranging from -2,147,483,648 to +2,147,483,647.
It’s called “pseudo-random” because the numbers appear random, but are really generated by
a logic operation that uses a “seed” value as a tap into a sequence of over 4 billion essentially
random numbers. If the same seed value is used again, the same sequence of numbers is
generated. The Propeller chip’s Random output is reversible; in fact, specifically it is a 32-bit
maximum-length, four-tap LFSR (Linear Feedback Shift Register) with taps in both the LSB
(Least Significant Bit, rightmost bit) and the MSB (Most Significant Bit, leftmost bit)
allowing for bi-directional operation.
Think of the pseudo-random sequence it generates as simply a static list of over 4 billion
numbers. Starting with a particular seed value and moving forward results in a list of a
specific set of numbers. If, however, you took that last number generated and used it as the
first seed value moving backward, you would end up with a list of the same numbers as
before, but in the reverse order. This is handy in many applications.
Here’s an example:
?X
The above shows the Random forward form; it uses X’s current value to retrieve the next
pseudo-random number in the forward direction and stores that number back in
X. Executing
?X again results in yet a different number, again stored back into X.
X?
The above shows the Random reverse form; it uses X’s current value to retrieve the next
pseudo-random number in the reverse direction and stores that number back in
X. Executing
X? again results in yet a different number, again stored back into X.
Since Random is always an assignment operator, the rules of Intermediate Assignments apply
to it (see page 147).
Propeller Manual v1.1 · Page 159