• Main Page
  • Modules
  • Classes
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/include/Random.h

Go to the documentation of this file.
00001 /* $Revision: 1.5 $ $Date: 2006/09/08 18:56:52 $ $Author: dgl $ $Name:  $ $Id: Random.h,v 1.5 2006/09/08 18:56:52 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 
00030 const Integer Musimat_Random_Seed = 2147483647;
00031 
00032 class Random {
00033 
00034 public:
00035 
00037         Random(Integer a = 16807, Integer b = 0, Integer c = Musimat_Random_Seed) : m_a(a), m_b(b), m_c(c) { m_x = 1; }
00038 
00040         Integer lCRandom( ) {
00041                 m_x = mod( m_a * m_x + m_b, m_c );      // update x based on its previous value
00042                 Integer r = m_x;
00043                 If ( r < 0 )                                            // force the result to be positive                                                      
00044                         r = -r; 
00045                 Return( r ); 
00046         }
00047 
00052         Real random( Real L, Real U ) {
00053                 Real r = lCRandom();
00054                 r = r / Real( m_c );
00055                 Return( r * ( U - L ) + L );
00056         }
00057 
00062         Integer random( Integer L, Integer U ) {
00063                 Real i = random( Real( L ), U + 1.0 );
00064                 Return( Integer( i ) );
00065         }
00066 
00070         Integer seed( Integer s ) {
00071                 Integer r = m_x;
00072                 m_x = s;
00073                 Return( r );
00074         }
00075 
00078         Integer getTime();
00079 
00080 private:
00081         Const Integer m_a;
00082         Const Integer m_b;
00083         Const Integer m_c;
00084         Integer m_x;                                            
00085 
00086 # if 0
00087         // This is the didactic method for modulo arithmetic described in "Musimathics"
00088         Integer mod( Integer j, Integer k ) {
00089                 While ( j >= k ) { j = j - k; }
00090                 While ( j <= -k ) { j = j + k; }
00091                 Return( j );
00092         }
00093 # else
00094         // We'll use the built-in mod operator instead for speed
00095         Integer mod( Integer j, Integer k ) {
00096                 Return j % k; 
00097         }
00098 #endif
00099 };
00100 
00102 Integer LCRandom( );
00103 
00105 Integer IRandom( Integer L, Integer U );
00106 
00108 Integer SeedRandom( Integer seed );
00109 
00112 Integer Time();
00113 
00115 Real Random( Real L = 0.0, Real U = 1.0 );
00116 
00118 Integer Random( Integer L, Integer U );
00119 
00120 #endif // RANDOM_H
00121 

Generated on Fri Nov 26 2010 16:18:25 for MusimatLib by  doxygen 1.7.2