#include "MusimatChapter9.h"
MusimatChapter9Section(C090703c) {
Print("*** Random Integer Numbers Scaled to an Arbitrary Range ***");
/*****************************************************************************

Random Integer Numbers Scaled to an Arbitrary Range

*****************************************************************************/
	para1(); // Step into this function to continue.
	para2(); // Step into this function to continue.
}

Static Void para1() {
/*****************************************************************************
We can adapt the Random() function to return integers within a specified 
integer range. When a real value is converted to an integer, we truncate 
(discard) the fractional part, leaving the integer part. For example,
*****************************************************************************/

	Print("*** Example of Truncation ***");
	Real r = 3.14159;
	Print("Real r = ", r);
	Integer i = Integer(r);
	Print("Truncated = ", i);

/*****************************************************************************
prints 3. Truncation is equivalent to the floor function.

Below is a method to generate integer random values over an integer range.
*****************************************************************************/
}

Integer Random(Integer L, Integer U) {
	Real rL = L;							// convert L to Real
	Real rU = U + 1.0;						// convert U to Real, add 1.0
	Real x = Random(rL, rU);				// get a real random value
	Return( Integer(x));					// return it as an integer
}

Static Void para2() {
/*****************************************************************************
Note that I added 1.0 to the upper real boundary. Truncation of the random 
result necessitates slightly increasing the top end of the range of choice. 
For example, in order to choose a value in the integer range 0 to 9, we must 
generate a random real value x that lies in the range 0.0 <= x < 10.0. This 
gives an equal chance of obtaining an integer in the range 0 to 9.

Here are ten values generated by this function over the interval [0,10].
*****************************************************************************/
	Print("*** Example of Random Integer Values in the Interval [0,10] ***");
	IntegerList x;

	For ( Integer i = 0; i < 10; i = i + 1 )
		x[i] = Random( 0, 10 );

	Print( x );

}}

//////////////////////////////////////////////////////////////////////////////////////////////////////
/* $Revision: 1.3 $ $Date: 2006/09/05 08:02:45 $ $Author: dgl $ $Name:  $ $Id: C090703c.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
