libStatGen Software  1
Random.h
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 //////////////////////////////////////////////////////////////////////////////
19 // This file includes code derived from the original Mersenne Twister Code
20 // by Makoto Matsumoto and Takuji Nishimura
21 // and is subject to their original copyright notice copied below:
22 //////////////////////////////////////////////////////////////////////////////
23 
24 //////////////////////////////////////////////////////////////////////////////
25 // COPYRIGHT NOTICE FOR MERSENNE TWISTER CODE
26 //
27 // Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
28 // All rights reserved.
29 //
30 // Redistribution and use in source and binary forms, with or without
31 // modification, are permitted provided that the following conditions
32 // are met:
33 //
34 // 1. Redistributions of source code must retain the above copyright
35 // notice, this list of conditions and the following disclaimer.
36 //
37 // 2. Redistributions in binary form must reproduce the above copyright
38 // notice, this list of conditions and the following disclaimer in the
39 // documentation and/or other materials provided with the distribution.
40 //
41 // 3. The names of its contributors may not be used to endorse or promote
42 // products derived from this software without specific prior written
43 // permission.
44 //
45 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
49 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 //
57 ///////////////////////////////////////////////////////////////////////////////
58 
59 
60 #ifndef __RANDOM_H__
61 #define __RANDOM_H__
62 
63 // Define a quick and dirty generator
64 #define RANDMUL 1664525L
65 #define RANDADD 1013904223L
66 
67 #define RAND(seed) ((seed = seed * RANDMUL + RANDADD) & 0xFFFFFFFF)
68 
69 class Random
70 // Implements the Mersenne Twister as default random number generator.
71 // Compilation flag __NO_MERSENNE sets default generator to
72 // a minimal Park-Miller with Bays-Durham shuffle and added safe guards.
73 {
74 protected:
75  // values for "minimal random values"
76  long seed;
77  long last;
78  long * shuffler;
79 
80  // and for normal deviates
81  int normSaved;
82  double normStore;
83 
84  double mersenneMult;
85 
86  // Array for Mersenne state vector
87  unsigned long * mt;
88 
89  // Used to signal that Mersenne state vector is not initialized
90  int mti;
91 
92 
93 public:
94 
95  Random(long s = 0x7654321);
96  ~Random();
97 
98  // Next bit in series of 0s and 1s
99  int Binary(); // Next bit in series of 0s and 1s
100 
101  // Next value in series, between 0 and 1
102  double Next();
103 
104  // Next integer
105  unsigned long NextInt();
106 
107  // Random number form N(0,1)
108  double Normal();
109 
110  void Reset(long s);
111  void InitMersenne(unsigned long s);
112 
113  // Random number between 0 and 1
114  operator double()
115  {
116  return Next();
117  }
118 
119  // Random number between arbitrary bounds
120  double Uniform(double lo = 0.0, double hi = 1.0)
121  {
122  return lo + (hi - lo) * Next();
123  }
124 
125  void Choose(int * array, int n, int k);
126  void Choose(int * array, float * weights, int n, int k);
127 
128 };
129 
130 extern Random globalRandom;
131 
132 #endif
133 
Definition: Random.h:73