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