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
Содержание BASIC Stamp 2e
Страница 1: ...BASIC Stamp Programming Manual Version 2 0c...
Страница 30: ...Introduction to the BASIC Stamps Page 28 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 90: ...BUTTON BASIC Stamp Command Reference Page 88 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 118: ...END BASIC Stamp Command Reference Page 116 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 128: ...FREQOUT BASIC Stamp Command Reference Page 126 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 196: ...NAP BASIC Stamp Command Reference Page 194 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 206: ...OWIN BASIC Stamp Command Reference Page 204 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 214: ...OWOUT BASIC Stamp Command Reference Page 212 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 216: ...PAUSE BASIC Stamp Command Reference Page 214 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 226: ...POLLMODE BASIC Stamp Command Reference Page 224 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 232: ...POLLOUT BASIC Stamp Command Reference Page 230 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 236: ...POLLRUN BASIC Stamp Command Reference Page 234 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 240: ...POLLWAIT BASIC Stamp Command Reference Page 238 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 262: ...RCTIME BASIC Stamp Command Reference Page 260 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 274: ...RUN BASIC Stamp Command Reference Page 272 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 310: ...SEROUT BASIC Stamp Command Reference Page 308 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 324: ...STOP BASIC Stamp Command Reference Page 322 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...