Randomness
Another important function is the random number generator, random(). Randomness isimportant in computer science for the purpose of running realistic simulations, for security,
and for introducing unpredictability in game play.
Each time the random() function is called, it returns a positive integer between 0 and
its single argument. For example, random(10) will return a number between 0 and 10
inclusively each time it is called. If we only wanted a random number between 1 and 10,
we would use the expression random(9) + 1. The expression random(100)-50 will return
an integer between -50 and 50 inclusively. The maximum range of the random() function
is 32767.
To generate random
oat type values between 0 and 1 inclusively, we can use the expres-
sion random(32767)/(float)32767. Here we use the maximum possible range so that we
get as many possibilities between 0 and 1 as we can.
Since computers are completely deterministic, getting randomness can be dicult. In
many programming environments (not RobotC) careful analysis of the random() function
will show that it generates the same sequence of random numbers every time you restart your
program. To change the sequence, programmers must seed the random number generator
with some externally obtained (and hopefully) random number. The seed of a random
number generator is the number that the generator starts with when applying its randomness
formula for computing the next random number. To set the seed, we call the srand()
function with an integer argument, just once, at the beginning of the program. Afterwards,
the random() function will generate a sequence of random numbers based on the seed.
Fortunately, robots have lots of external sources for seeds. The programmer could read
the sound sensor and use that value as the seed before proceeding. The sequence of random
numbers would then depend upon the sound level at the time the program was started.
In a noisy room, this would be a good source of randomness. However, in a quiet room,
the reading may be the same each time, which gives the same random sequence each time
the program runs. To get around this, there are lots of possibilities. The programmer
could read numbers from several sensors and mix the values together in a formula. A very
reliable random seed is value of the system clock when the program is executed. With this
method, each time the program runs, a dierent seed, based on the system clock, will be
used and, in turn, a dierent sequence of random numbers will be generated. The reserved
variable, nSysTime, contains an ever-changing integer that represents the number of elapsed
milliseconds since the brick was powered up. More details about system time are available
in the RobotC On-line Support on the left side-bar under the NXT Functions ! Timing
section. This method is so reliable, in fact, that the RobotC developers decided to make
it the default behavior, so seeding will not be an issue in RobotC{lucky us. Years ago
developers as Silicon Graphics Inc (SGI), desperate for good random seeds, used live images
of lava lamps to generate truly random seeds3.
No comments:
Post a Comment