EasyManua.ls Logo

Parallax BASIC Stamp 2e - Page 254

Default Icon
353 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
RANDOM - BASIC Stamp Command Reference
Page 252 BASIC Stamp Programming Manual 2.0b www.parallaxinc.com
SYMBOL Result = W0
Result = 11000 ' Set initial "seed" value
Loop:
RANDOM Result ' Generate random number.
DEBUG Result ' Show the result on screen.
GOTO Loop
-- or --
Result VAR WORD
Result = 11000 ' Set initial "seed" value
Loop:
RANDOM Result ' Generate random number
DEBUG DEC ? Result ' Show the result on screen.
GOTO Loop
Here, Result is only initialized once, before the loop. Each time through
the loop, the previous value of Result, generated by RANDOM, is used as
the next seed value. This generates a more desirable set of pseudorandom
numbers.
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.
Another possibility is to take advantage of the "floating" effect of unused
input pins. Because any I/O pin that is an input, and is not electrically
connected to anything, tends to "float" randomly between 0 and 1, this is a
good source of a potential seed value. For example, if the upper 8 pins on
a BS2 are not being used, leave them as inputs and don't electrically
connect them (leave them "floating"). Then, use something like the
following code to initialize the seed value:
Result = INH * 256 + INH ' Fill high and low byte with current, floating,
' value of I/O pins 8 - 15
1
1
NOTE: BS1's only have 8 I/O pins.
There may not be enough unused
pins to do something similar, but if
so, use the PINS variable, rather
than INH.
2
e
2
sx
2
p
2

Table of Contents

Related product manuals