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