• Main Page
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/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          Seeding the Random Number Generator
00006          
00007          Unlike the natural sources of randomness, LCRandom() will always produce the 
00008          same sequence with the same initial parameters. 
00009          
00010          Different sets of pseudorandom 
00011          sequences can be generated by varying the initial value of x, as with the 
00012          SeedRandom() function defined below.
00013          *****************************************************************************/
00014         para1(); // Step into this function to continue.
00015 }
00016 
00017 Integer lcRandom();
00018 extern Integer lc_x;
00019 
00020 /*****************************************************************************
00021  seedRandom() is a simple function to seed the random number generator.
00022  This function is a copy of SeedRandom() defined in Random.cpp, reproduced here
00023  for dydactic reasons (to keep things simple). 
00024  lc_x is a global Integer, again for dydactic reasons.
00025  *****************************************************************************/
00026 
00027 Void seedRandom(Integer s) { 
00028         lc_x = s; // set global variable lc_x to seed s
00029 }
00030 
00031 Static Void para1() {
00032         /*****************************************************************************
00033          Below is an example of generating random numbers from a seed value of 100,
00034          resetting the seed to 100, and generating the same three random numbers.
00035          Repeatability is why this is called a pseudorandom number generator.
00036          *****************************************************************************/
00037         
00038         Print("*** Three random numbers based on seed 100 ***");
00039         seedRandom( 100 );
00040         For (Integer i = 0; i < 3; i++ ) {
00041                 Print( lcRandom() );
00042         }
00043         Print("Same three random numbers after resetting the seed to 100:");
00044         seedRandom( 100 );
00045         For (Integer i = 0; i < 3; i++ ) {
00046                 Print( lcRandom() );
00047         }
00048         
00049         /*****************************************************************************
00050          The seedRandom() function allows us to set the initial value of x. If we initialize x to a 
00051          parameter such as the current time in seconds from some fixed moment, then we 
00052          start at a different place in the pseudorandom cycle each time (although, of 
00053          course, this is finite, too, because the sequence length is necessarily limited).
00054          *****************************************************************************/
00055         
00056         Print("LCRandom seeded with current time in seconds:");
00057         seedRandom( Time() );
00058         For (Integer i = 0; i < 3; i++ ) {
00059                 Print( lcRandom() );
00060         }
00061         
00062         /*****************************************************************************
00063          The linear congruential method is simple and efficient, but it is hardly the 
00064          best source of random values. Even ignoring the fact that it repeats, its 
00065          uniformity is not wonderful. Knuth (1973, vol. 2) cautioned, "Random number 
00066          generators should not be chosen at random." For superior techniques, see 
00067          Press et al. (1988, 210). However, this method is very simple to implement 
00068          and has the advantage over natural random processes of providing the same 
00069          pseudorandom sequence if seeded with the same values.
00070          *****************************************************************************/
00071 }
00072 
00074 /* $Revision: 1.3 $ $Date: 2006/09/05 08:02:45 $ $Author: dgl $ $Name:  $ $Id: C090703a.cpp,v 1.3 2006/09/05 08:02:45 dgl Exp $ */
00075 // The Musimat Tutorial © 2006 Gareth Loy
00076 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00077 // and published exclusively by The MIT Press.
00078 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00079 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00080 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00081 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00082 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00083 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00084 // The Musimat website:                 http://www.musimat.com/
00085 // This program is released under the terms of the GNU General Public License
00086 // available here:                      http://www.gnu.org/licenses/gpl.txt

Generated on Fri Nov 26 2010 16:18:24 for Musimat Chapter 9 Code Examples by  doxygen 1.7.2