#include "MusimatChapter9.h"
Go to the source code of this file.
Functions | |
MusimatChapter9Section (C090703a) | |
Integer | lcRandom () |
Void | seedRandom (Integer s) |
Static Void | para1 () |
Variables | |
Integer | lc_x |
Integer lcRandom | ( | ) |
MusimatChapter9Section | ( | C090703a | ) |
Definition at line 2 of file C090703a.cpp.
References para1().
{ Print("*** Seeding the Random Number Generator ***"); /***************************************************************************** Seeding the Random Number Generator Unlike the natural sources of randomness, LCRandom() will always produce the same sequence with the same initial parameters. Different sets of pseudorandom sequences can be generated by varying the initial value of x, as with the SeedRandom() function defined below. *****************************************************************************/ para1(); // Step into this function to continue. }
Static Void para1 | ( | ) |
Definition at line 31 of file C090703a.cpp.
References lcRandom(), and seedRandom().
{ /***************************************************************************** Below is an example of generating random numbers from a seed value of 100, resetting the seed to 100, and generating the same three random numbers. Repeatability is why this is called a pseudorandom number generator. *****************************************************************************/ Print("*** Three random numbers based on seed 100 ***"); seedRandom( 100 ); For (Integer i = 0; i < 3; i++ ) { Print( lcRandom() ); } Print("Same three random numbers after resetting the seed to 100:"); seedRandom( 100 ); For (Integer i = 0; i < 3; i++ ) { Print( lcRandom() ); } /***************************************************************************** The seedRandom() function allows us to set the initial value of x. If we initialize x to a parameter such as the current time in seconds from some fixed moment, then we start at a different place in the pseudorandom cycle each time (although, of course, this is finite, too, because the sequence length is necessarily limited). *****************************************************************************/ Print("LCRandom seeded with current time in seconds:"); seedRandom( Time() ); For (Integer i = 0; i < 3; i++ ) { Print( lcRandom() ); } /***************************************************************************** The linear congruential method is simple and efficient, but it is hardly the best source of random values. Even ignoring the fact that it repeats, its uniformity is not wonderful. Knuth (1973, vol. 2) cautioned, "Random number generators should not be chosen at random." For superior techniques, see Press et al. (1988, 210). However, this method is very simple to implement and has the advantage over natural random processes of providing the same pseudorandom sequence if seeded with the same values. *****************************************************************************/ }
Void seedRandom | ( | Integer | s ) |
Definition at line 27 of file C090703a.cpp.
References lc_x.
{ lc_x = s; // set global variable lc_x to seed s }
Integer lc_x |
Definition at line 53 of file C090703.cpp.