• Main Page
  • Files
  • File List
  • File Members

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

Go to the documentation of this file.
00001 #include "MusimatChapter9.h"
00002 MusimatChapter9Section(C090703b) {
00003         Print("*** Random Real Numbers ***");
00004         /*****************************************************************************
00005          
00006          Random Real Numbers
00007          
00008          The LCRandom() method returns integers between 0 and c. It is straightforward 
00009          to map its output to any range of Real values between an upper bound U and a 
00010          lower bound L, as shown in the Random function, below.
00011          *****************************************************************************/
00012         para1(); // Step into this function to continue.
00013 }
00014 
00015 /*****************************************************************************
00016  luRandom() is a copy of the Musimat Random() function defined in Random.cpp,
00017  reproduced here for dydactic reasons (to keep things simple).
00018  *****************************************************************************/
00019 Real luRandom(Real L = 0.0, Real U = 1.0) {
00020         Static Const Integer c = Musimat_Random_Seed; // defined in Random.h
00021         Integer i = LCRandom();                 // get a random integer value
00022         Real r = Real(i);                               // convert it to a real value
00023         r = r/Real(c);                                  // scale it to 0.0 <= r < 1.0
00024         Return(r * (U - L) + L);                // scale it to the range L to U
00025 }
00026 
00027 Static Void para1() {
00028         /*****************************************************************************
00029          First, we use LCRandom() to get a random integer. Recall that LCRandom() 
00030          forces the result to be positive. We promote its random integer result to 
00031          Real and store it in r. Next, we divide it by c so its range is 0.0 <= r < 1.0. 
00032          Finally, we scale it by the difference between U and L, and add L, so that 
00033          the random value is bounded above by U and below by L. That way we can get 
00034          a random result from a particular range of values that we can stipulate.
00035          
00036          Here is an example of invoking this Real Random() method:
00037          *****************************************************************************/
00038         
00039         Print("*** Ten invocations of luRandom() ***");
00040         RealList x;
00041         
00042         For (Integer i = 0; i < 10; i++ ) {
00043                 x[i] = luRandom(0.0, 1.0);
00044         }
00045         
00046         Print( x );
00047         
00048         /*****************************************************************************
00049          luRandom() is declared above with initializing values for its
00050          two arguments. This means luRandom() can be called with 0, 1, or 2 arguments.
00051          If called with zero arguments, both initial values are employed; if called with one
00052          argument, only the second initial value is employed; if called with two arguments,
00053          the initial values are ignored.
00054          Initial value for L defaults to 0.0, and U defaults to 1.0.  Thus, to
00055          get a number in the unsigned unit interval [0,1), it suffices to invoke 
00056          luRandom() without arguments, as follows:
00057          *****************************************************************************/
00058         Print("*** Various Calls to luRandom() ***");
00059         Real r = luRandom();
00060         Print(r);
00061         Print("luRandom(37.5, 37.9)=", luRandom(37.5, 37.9));
00062         
00063 }
00064 
00066 /* $Revision: 1.3 $ $Date: 2006/09/05 08:02:45 $ $Author: dgl $ $Name:  $ $Id: C090703b.cpp,v 1.3 2006/09/05 08:02:45 dgl Exp $ */
00067 // The Musimat Tutorial � 2006 Gareth Loy
00068 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" � 2006 Gareth Loy 
00069 // and published exclusively by The MIT Press.
00070 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00071 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00072 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00073 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00074 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00075 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00076 // The Musimat website:                 http://www.musimat.com/
00077 // This program is released under the terms of the GNU General Public License
00078 // 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