C:/Musimathics_local/Musimat/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 Real Random(Real L, Real U) {
00016         Integer i = LCRandom();                 // get a random integer value
00017         Real r = Real(i);                               // convert it to a real value
00018         r = r/Real(c);                                  // scale it to 0.0 <= r < 1.0
00019         Return(r * (U - L) + L);                // scale it to the range L to U
00020 }
00021 
00022 Static Void para1() {
00023 /*****************************************************************************
00024 First, we use LCRandom() to get a random integer. Recall that LCRandom() 
00025 forces the result to be positive. We promote its random integer result to 
00026 Real and store it in r. Next, we divide it by c so its range is 0.0 <= r < 1.0. 
00027 Finally, we scale it by the difference between U and L, and add L, so that 
00028 the random value is bounded above by U and below by L. That way we can get 
00029 a random result from a particular range of values that we can stipulate.
00030 
00031 Here is an example of invoking this Real Random() method:
00032 *****************************************************************************/
00033 
00034         Print("*** Ten invocations of Real Random() ***");
00035         RealList x;
00036         
00037         For (Integer i = 0; i < 10; i++ )
00038                 x[i] = Random(0.0, 1.0);
00039 
00040         Print( x );
00041 
00042 /*****************************************************************************
00043 This function is declared in MusimatChapter9.h with default values for its
00044 two arguments such that L defaults to 0.0 and U defaults to 1.0.  Thus, to
00045 get a number in the unsigned unit interval [0,1), it suffices to invoke 
00046 Random() without arguments, as follows:
00047 *****************************************************************************/
00048         Print("*** Various Calls to Real Random() ***");
00049         Real r = Random();
00050         Print(r);
00051         Print("Random(37.5, 37.9)=", Random(37.5, 37.9));
00052 
00053 }}
00054 
00056 /* $Revision: 1.4 $ $Date: 2006/09/12 17:37:25 $ $Author: dgl $ $Name:  $ $Id: _c090703b_8cpp-source.html,v 1.4 2006/09/12 17:37:25 dgl Exp $ */
00057 // The Musimat Tutorial © 2006 Gareth Loy
00058 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00059 // and published exclusively by The MIT Press.
00060 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00061 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00062 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00063 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00064 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00065 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00066 // The Musimat website:                 http://www.musimat.com/
00067 // This program is released under the terms of the GNU General Public License
00068 // available here:                      http://www.gnu.org/licenses/gpl.txt

Generated on Tue Sep 12 10:14:14 2006 for Musimat Chapter 9 Code Examples by  doxygen 1.4.7