- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/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 Below is a method to generate integer random values over an integer range.
    00030 *****************************************************************************/
    00031 }
    00032
    00033 Integer Random(Integer L, Integer U) {
    00034         Real rL = L;                                                    // convert L to Real
    00035         Real rU = U + 1.0;                                              // convert U to Real, add 1.0
    00036         Real x = Random(rL, rU);                                // get a real random value
    00037         Return( Integer(x));                                    // return it as an integer
    00038 }
    00039
    00040 Static Void para2() {
    00041 /*****************************************************************************
    00042 Note that I added 1.0 to the upper real boundary. Truncation of the random
    00043 result necessitates slightly increasing the top end of the range of choice.
    00044 For example, in order to choose a value in the integer range 0 to 9, we must
    00045 generate a random real value x that lies in the range 0.0 <= x < 10.0. This
    00046 gives an equal chance of obtaining an integer in the range 0 to 9.
    00047
    00048 Here are ten values generated by this function over the interval [0,10].
    00049 *****************************************************************************/
    00050         Print("*** Example of Random Integer Values in the Interval [0,10] ***");
    00051         IntegerList x;
    00052
    00053         For ( Integer i = 0; i < 10; i = i + 1 )
    00054                 x[i] = Random( 0, 10 );
    00055
    00056         Print( x );
    00057
    00058 }}
    00059
    00061 /* $Revision: 1.2 $ $Date: 2006/09/09 06:22:41 $ $Author: dgl $ $Name:  $ $Id: _c090703c_8cpp-source.html,v 1.2 2006/09/09 06:22:41 dgl Exp $ */
    00062 // The Musimat Tutorial © 2006 Gareth Loy
    00063 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00064 // and published exclusively by The MIT Press.
    00065 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00066 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00067 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00068 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00069 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00070 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00071 // The Musimat website:                 http://www.musimat.com/
    00072 // This program is released under the terms of the GNU General Public License
    00073 // available here:                      http://www.gnu.org/licenses/gpl.txt