C:/Musimathics_local/Musimat/MusimatChapter9/C090703a.cpp

Go to the documentation of this file.
00001 #include "MusimatChapter9.h"
00002 MusimatChapter9Section(C090703a) {
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 }
00017 
00018 Void SeedRandom(Integer s) { 
00019         x = s; // set global variable x to seed s
00020 } 
00021 
00022 Static Void para1() {
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 }}
00060 
00062 /* $Revision: 1.4 $ $Date: 2006/09/12 17:37:24 $ $Author: dgl $ $Name:  $ $Id: _c090703a_8cpp-source.html,v 1.4 2006/09/12 17:37:24 dgl Exp $ */
00063 // The Musimat Tutorial © 2006 Gareth Loy
00064 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00065 // and published exclusively by The MIT Press.
00066 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00067 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00068 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00069 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00070 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00071 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00072 // The Musimat website:                 http://www.musimat.com/
00073 // This program is released under the terms of the GNU General Public License
00074 // available here:                      http://www.gnu.org/licenses/gpl.txt

Generated on Tue Sep 12 10:14:14 2006 for Musimat Chapter 9 Code Examples by  doxygen 1.4.7