#include "MusimatChapter9.h"Go to the source code of this file.
Functions | |
| MusimatChapter9Section (C090703a) | |
| Void | SeedRandom (Integer s) |
| Static Void | para1 () |
| MusimatChapter9Section | ( | C090703a | ) |
Definition at line 2 of file C090703a.cpp.
References para1().
00002 { 00003 Print("*** Seeding the Random Number Generator ***"); 00004 /***************************************************************************** 00005 00006 Seeding the Random Number Generator 00007 00008 Unlike the natural sources of randomness, LCRandom() will always produce the 00009 same sequence with the same initial parameters. 00010 00011 Different sets of pseudorandom 00012 sequences can be generated by varying the initial value of x, as with the 00013 SeedRandom() function defined below. 00014 *****************************************************************************/ 00015 para1(); // Step into this function to continue. 00016 }
| Static Void para1 | ( | ) |
Definition at line 22 of file C090703a.cpp.
References LCRandom(), and SeedRandom().
00022 { 00023 /***************************************************************************** 00024 Below is an example of generating random numbers from a seed value of 100, 00025 resetting the seed to 100, and generating the same three random numbers. 00026 Repeatability is why this is called a pseudorandom number generator. 00027 *****************************************************************************/ 00028 00029 Print("*** Three random numbers based on seed 100 ***"); 00030 SeedRandom( 100 ); 00031 For (Integer i = 0; i < 3; i++ ) 00032 Print( LCRandom() ); 00033 Print("Same three random numbers after resetting the seed to 100:"); 00034 SeedRandom( 100 ); 00035 For (Integer i = 0; i < 3; i++ ) 00036 Print( LCRandom() ); 00037 00038 /***************************************************************************** 00039 The SeedRandom() function allows us to set the initial value of x. If we initialize x to a 00040 parameter such as the current time in seconds from some fixed moment, then we 00041 start at a different place in the pseudorandom cycle each time (although, of 00042 course, this is finite, too, because the sequence length is necessarily limited). 00043 *****************************************************************************/ 00044 00045 Print("LCRandom seeded with current time in seconds:"); 00046 SeedRandom( Time() ); 00047 For (Integer i = 0; i < 3; i++ ) 00048 Print( LCRandom() ); 00049 00050 /***************************************************************************** 00051 The linear congruential method is simple and efficient, but it is hardly the 00052 best source of random values. Even ignoring the fact that it repeats, its 00053 uniformity is not wonderful. Knuth (1973, vol. 2) cautioned, “Random number 00054 generators should not be chosen at random.” For superior techniques, see 00055 Press et al. (1988, 210). However, this method is very simple to implement 00056 and has the advantage over natural random processes of providing the same 00057 pseudorandom sequence if seeded with the same values. 00058 *****************************************************************************/ 00059 }}
| Void SeedRandom | ( | Integer | s | ) |
Definition at line 18 of file C090703a.cpp.
References x.
00018 { 00019 x = s; // set global variable x to seed s 00020 }
1.4.7