#include "MusimatChapter9.h"
MusimatChapter9Section(C090703a) {
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.
}

Void SeedRandom(Integer s) { 
	x = s; // set global variable x to seed s
} 

Static Void para1() {
/*****************************************************************************
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.
*****************************************************************************/
}}

//////////////////////////////////////////////////////////////////////////////////////////////////////
/* $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 $ */
// The Musimat Tutorial © 2006 Gareth Loy
// Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
// and published exclusively by The MIT Press.
// This program is released WITHOUT ANY WARRANTY; without even the implied 
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
// "Musimathics" is available here:	http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
// Gareth Loy's Musimathics website:	http://www.musimathics.com/
// The Musimat website:			http://www.musimat.com/
// This program is released under the terms of the GNU General Public License
// available here:			http://www.gnu.org/licenses/gpl.txt
