• Main Page
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/MusimatChapter9/C090703c.cpp

Go to the documentation of this file.
00001 #include "MusimatChapter9.h"
00002 MusimatChapter9Section(C090703c) {
00003         Print("*** Random Integer Numbers Scaled to an Arbitrary Range ***");
00004         /*****************************************************************************
00005          
00006          Random Integer Numbers Scaled to an Arbitrary Range
00007          
00008          *****************************************************************************/
00009         para1(); // Step into this function to continue.
00010         para2(); // Step into this function to continue.
00011 }
00012 
00013 Static Void para1() {
00014         /*****************************************************************************
00015          We can adapt the Random() function to return integers within a specified 
00016          integer range. When a real value is converted to an integer, we truncate 
00017          (discard) the fractional part, leaving the integer part. For example,
00018          *****************************************************************************/
00019         
00020         Print("*** Example of Truncation ***");
00021         Real r = 3.14159;
00022         Print("Real r = ", r);
00023         Integer i = Integer(r);
00024         Print("Truncated = ", i);
00025         
00026         /*****************************************************************************
00027          prints 3. Truncation is equivalent to the floor function.
00028          
00029          The iRandom() function below is a method to generate integer random values over 
00030          an integer range. It uses the Musimat library Real Random(Real, Real) function 
00031          to return a Real numerical range, then truncates the result to an integer by type 
00032          casting the result to type Integer. This behavior is identical to the Musimat
00033          function Integer Random(Integer, Integer), but is reproduced here for dydactic
00034          reasons (to keep things simple).
00035          *****************************************************************************/
00036 }
00037 
00038 Integer iRandom(Integer L, Integer U) {
00039         Real rL = L;                                                    // convert L to Real
00040         Real rU = U + 1.0;                                              // convert U to Real, add 1.0
00041         Real x = Random(rL, rU);                                // get a Real random value
00042         Return( Integer(x) );                                   // return result as an Integer
00043 }
00044 
00045 Static Void para2() {
00046         /*****************************************************************************
00047          Note that I added 1.0 to the upper real boundary in iRandom(). Truncation of the random 
00048          result necessitates slightly increasing the top end of the range of choice. 
00049          For example, in order to choose a value in the integer range 0 to 9, we must 
00050          generate a random real value x that lies in the range 0.0 <= x < 10.0. This 
00051          gives an equal chance of obtaining an integer in the range 0 to 9.
00052          
00053          Here are ten values generated by iRandom() over the interval [0,10].
00054          *****************************************************************************/
00055         Print("*** Example of Random Integer Values in the Interval [0,10] ***");
00056         IntegerList x;
00057         
00058         For ( Integer i = 0; i < 10; i = i + 1 ) {
00059                 x[i] = iRandom( 0, 10 );
00060         }
00061         
00062         Print( x );
00063         
00064 }
00065 
00067 /* $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 $ */
00068 // The Musimat Tutorial � 2006 Gareth Loy
00069 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" � 2006 Gareth Loy 
00070 // and published exclusively by The MIT Press.
00071 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00072 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00073 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00074 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00075 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00076 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00077 // The Musimat website:                 http://www.musimat.com/
00078 // This program is released under the terms of the GNU General Public License
00079 // 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