00001 00002 ////////////////////////////////////////////////////////////////////////////// 00003 // This file includes code derived from the original Mersenne Twister Code 00004 // by Makoto Matsumoto and Takuji Nishimura 00005 // and is subject to their original copyright notice copied below: 00006 ////////////////////////////////////////////////////////////////////////////// 00007 00008 ////////////////////////////////////////////////////////////////////////////// 00009 // COPYRIGHT NOTICE FOR MERSENNE TWISTER CODE 00010 // 00011 // Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, 00012 // All rights reserved. 00013 // 00014 // Redistribution and use in source and binary forms, with or without 00015 // modification, are permitted provided that the following conditions 00016 // are met: 00017 // 00018 // 1. Redistributions of source code must retain the above copyright 00019 // notice, this list of conditions and the following disclaimer. 00020 // 00021 // 2. Redistributions in binary form must reproduce the above copyright 00022 // notice, this list of conditions and the following disclaimer in the 00023 // documentation and/or other materials provided with the distribution. 00024 // 00025 // 3. The names of its contributors may not be used to endorse or promote 00026 // products derived from this software without specific prior written 00027 // permission. 00028 // 00029 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00030 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00031 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00032 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00033 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00034 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00035 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00036 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00037 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00038 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00039 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00040 // 00041 /////////////////////////////////////////////////////////////////////////////// 00042 00043 00044 #ifndef __RANDOM_H__ 00045 #define __RANDOM_H__ 00046 00047 // Define a quick and dirty generator 00048 #define RANDMUL 1664525L 00049 #define RANDADD 1013904223L 00050 00051 #define RAND(seed) ((seed = seed * RANDMUL + RANDADD) & 0xFFFFFFFF) 00052 00053 class Random 00054 // Implements the Mersenne Twister as default random number generator. 00055 // Compilation flag __NO_MERSENNE sets default generator to 00056 // a minimal Park-Miller with Bays-Durham shuffle and added safe guards. 00057 { 00058 protected: 00059 // values for "minimal random values" 00060 long seed; 00061 long last; 00062 long * shuffler; 00063 00064 // and for normal deviates 00065 int normSaved; 00066 double normStore; 00067 00068 double mersenneMult; 00069 00070 // Array for Mersenne state vector 00071 unsigned long * mt; 00072 00073 // Used to signal that Mersenne state vector is not initialized 00074 int mti; 00075 00076 00077 public: 00078 00079 Random(long s = 0x7654321); 00080 ~Random(); 00081 00082 // Next bit in series of 0s and 1s 00083 int Binary(); // Next bit in series of 0s and 1s 00084 00085 // Next value in series, between 0 and 1 00086 double Next(); 00087 00088 // Next integer 00089 unsigned long NextInt(); 00090 00091 // Random number form N(0,1) 00092 double Normal(); 00093 00094 void Reset(long s); 00095 void InitMersenne(unsigned long s); 00096 00097 // Random number between 0 and 1 00098 operator double() 00099 { 00100 return Next(); 00101 } 00102 00103 // Random number between arbitrary bounds 00104 double Uniform(double lo = 0.0, double hi = 1.0) 00105 { 00106 return lo + (hi - lo) * Next(); 00107 } 00108 00109 void Choose(int * array, int n, int k); 00110 void Choose(int * array, float * weights, int n, int k); 00111 00112 }; 00113 00114 extern Random globalRandom; 00115 00116 #endif 00117