MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RandMT.h
1 // This is the ``Mersenne Twister'' random number generator MT19937, which
2 // generates pseudorandom integers uniformly distributed in 0..(2^32 - 1)
3 // starting from any odd seed in 0..(2^32 - 1). This version is a recode
4 // by Shawn Cokus (Cokus@math.washington.edu) on March 8, 1998 of a version by
5 // Takuji Nishimura (who had suggestions from Topher Cooper and Marc Rieffel in
6 // July-August 1997).
7 //
8 // Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC Alpha
9 // running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to
10 // generate 300 million random numbers; after recoding: 24.0 sec. for the same
11 // (i.e., 46.5% of original time), so speed is now about 12.5 million random
12 // number generations per second on this machine.
13 //
14 // According to the URL <http://www.math.keio.ac.jp/~matumoto/emt.html>
15 // (and paraphrasing a bit in places), the Mersenne Twister is ``designed
16 // with consideration of the flaws of various existing generators,'' has
17 // a period of 2^19937 - 1, gives a sequence that is 623-dimensionally
18 // equidistributed, and ``has passed many stringent tests, including the
19 // die-hard test of G. Marsaglia and the load test of P. Hellekalek and
20 // S. Wegenkittl.'' It is efficient in memory usage (typically using 2506
21 // to 5012 bytes of static data, depending on data type sizes, and the code
22 // is quite short as well). It generates random numbers in batches of 624
23 // at a time, so the caching and pipelining of modern systems is exploited.
24 // It is also divide- and mod-free.
25 //
26 // This library is free software; you can redistribute it and/or modify it
27 // under the terms of the GNU Library General Public License as published by
28 // the Free Software Foundation (either version 2 of the License or, at your
29 // option, any later version). This library is distributed in the hope that
30 // it will be useful, but WITHOUT ANY WARRANTY, without even the implied
31 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
32 // the GNU Library General Public License for more details. You should have
33 // received a copy of the GNU Library General Public License along with this
34 // library; if not, write to the Free Software Foundation, Inc., 59 Temple
35 // Place, Suite 330, Boston, MA 02111-1307, USA.
36 //
37 // The code as Shawn received it included the following notice:
38 //
39 // Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When
40 // you use this, send an e-mail to <matumoto@math.keio.ac.jp> with
41 // an appropriate reference to your work.
42 //
43 // It would be nice to CC: <Cokus@math.washington.edu> when you write.
44 //
45 // RandMT class created by Paul Gresham <gresham@mediavisual.com>
46 // There seems to be a slight performance deficit in process creation
47 // however I've not profiled the class to compare it with the straight
48 // C code.
49 //
50 // Use of a class removes many C nasties and also allows you to easily
51 // create multiple generators.
52 // To compile on GNU a simple line is:
53 // g++ -O3 RandMT.cc -o RandMT
54 //
55 
56 #ifndef _RANDMT_H_
57 #define _RANDMT_H_
58 
59 namespace MFM {
60 
61 typedef unsigned long uint32;
62 
63 class RandMT {
64 
65  static const int N = 624; // length of state vector
66  static const int M = 397; // a period parameter
67  static const uint32 K = 0x9908B0DFU; // a magic constant
68 
69  // If you want a single generator, consider using a singleton class
70  // instead of trying to make these static.
71  uint32 state[N+1]; // state vector + 1 extra to not violate ANSI C
72  uint32 *next; // next random value is computed from here
73  uint32 initseed; //
74  int left; // can *next++ this many times before reloading
75 
76  inline uint32 hiBit(uint32 u) {
77  return u & 0x80000000U; // mask all but highest bit of u
78  }
79 
80  inline uint32 loBit(uint32 u) {
81  return u & 0x00000001U; // mask all but lowest bit of u
82  }
83 
84  inline uint32 loBits(uint32 u) {
85  return u & 0x7FFFFFFFU; // mask the highest bit of u
86  }
87 
88  inline uint32 mixBits(uint32 u, uint32 v) {
89  return hiBit(u)|loBits(v); // move hi bit of u to hi bit of v
90  }
91 
92  uint32 reloadMT(void) ;
93 
94  // MUST NOT PASS SEQUENTIAL SEEDS TO THIS VERSION
95  void seedMT(uint32 seed) ;
96 
97 public:
98  RandMT() ;
99  RandMT(uint32 seed) ;
100  inline uint32 randomMT(void) ;
101 
102  // ACKLEYHAX: This seeding function hacked for MFM
103  void seedMT_MFM(uint32 s) ;
104 
105 };
106 
107 inline uint32 RandMT::randomMT(void) {
108  uint32 y;
109 
110  if(--left < 0)
111  return(reloadMT());
112 
113  y = *next++;
114  y ^= (y >> 11);
115  y ^= (y << 7) & 0x9D2C5680U;
116  y ^= (y << 15) & 0xEFC60000U;
117  return(y ^ (y >> 18));
118 }
119 } /* namespace MFM */
120 
121 #endif // _RANDMT_H_
122 
123 
124 
Definition: RandMT.h:63