C:/Musimathics_local/Musimat/include/Random.h

Go to the documentation of this file.
00001 /* $Revision: 1.4 $ $Date: 2006/09/12 17:37:41 $ $Author: dgl $ $Name:  $ $Id: _random_8h-source.html,v 1.4 2006/09/12 17:37:41 dgl Exp $ */
00002 #ifndef RANDOM_H
00003 #define RANDOM_H
00004 
00005 // The Musimat Tutorial © 2006 Gareth Loy
00006 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00007 // and published exclusively by The MIT Press.
00008 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00009 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00010 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00011 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00012 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00013 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00014 // The Musimat website:                 http://www.musimat.com/
00015 // This program is released under the terms of the GNU General Public License
00016 // available here:                      http://www.gnu.org/licenses/gpl.txt
00017 
00018 #include "Aliases.h"
00019 
00023 
00024 
00029 class Random {
00030 
00031 public:
00032 
00034         Random(Integer a = 16807, Integer b = 0, Integer c = 2147483647) : m_a(a), m_b(b), m_c(c) { m_x = 1; }
00035 
00037         Integer lCRandom( ) {
00038                 m_x = mod( m_a * m_x + m_b, m_c );      // update x based on its previous value
00039                 Integer r = m_x;
00040                 If ( r < 0 )                                            // force the result to be positive                                                      
00041                         r = -r; 
00042                 Return( r ); 
00043         }
00044 
00049         Real random( Real L, Real U ) {
00050                 Real r = lCRandom();
00051                 r = r / Real( m_c );
00052                 Return( r * ( U - L ) + L );
00053         }
00054 
00059         Integer random( Integer L, Integer U ) {
00060                 Real i = random( Real( L ), U + 1.0 );
00061                 Return( Integer( i ) );
00062         }
00063 
00067         Integer seed( Integer s ) {
00068                 Integer r = m_x;
00069                 m_x = s;
00070                 Return( r );
00071         }
00072 
00075         Integer getTime();
00076 
00077 private:
00078         Const Integer m_a;
00079         Const Integer m_b;
00080         Const Integer m_c;
00081         Integer m_x;                                            
00082 
00083 # if 0
00084         // This is the didactic method for modulo arithmetic described in "Musimathics"
00085         Integer mod( Integer j, Integer k ) {
00086                 While ( j >= k ) { j = j - k; }
00087                 While ( j <= -k ) { j = j + k; }
00088                 Return( j );
00089         }
00090 # else
00091         // We'll use the built-in mod operator instead for speed
00092         Integer mod( Integer j, Integer k ) {
00093                 Return j % k; 
00094         }
00095 #endif
00096 };
00097 
00099 Integer LCRandom( );
00100 
00102 Integer IRandom( Integer L, Integer U );
00103 
00105 Integer SeedRandom( Integer seed );
00106 
00109 Integer Time();
00110 
00112 Real Random( Real L = 0.0, Real U = 1.0 );
00113 
00115 Integer Random( Integer L, Integer U );
00116 
00117 #endif // RANDOM_H
00118 

Generated on Tue Sep 12 10:14:18 2006 for MusimatLib by  doxygen 1.4.7