wfmath  1.0.3
A math library for the Worldforge system.
randgen.cpp
1 // randgen.cpp (time and random number implementation)
2 //
3 // The WorldForge Project
4 // Copyright (C) 2002, 2013 The WorldForge Project
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // For information about WorldForge and its authors, please contact
21 // the Worldforge Web Site at http://www.worldforge.org.
22 //
23 // Portions of the code in this file were copied and modified from
24 // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
25 //
26 // Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
27 // All rights reserved.
28 //
29 // Redistribution and use in source and binary forms, with or without
30 // modification, are permitted provided that the following conditions
31 // are met:
32 //
33 // 1. Redistributions of source code must retain the above copyright
34 // notice, this list of conditions and the following disclaimer.
35 //
36 // 2. Redistributions in binary form must reproduce the above copyright
37 // notice, this list of conditions and the following disclaimer in the
38 // documentation and/or other materials provided with the distribution.
39 //
40 // 3. The names of its contributors may not be used to endorse or promote
41 // products derived from this software without specific prior written
42 // permission.
43 //
44 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
47 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
48 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
49 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
50 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
51 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
52 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
53 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
54 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 //
56 
57 // Original Author: Ron Steinke
58 // Created: 2002-5-23
59 
60 #include "randgen.h"
61 #include <ctime>
62 #include <cstdio>
63 #include <istream>
64 #include <ostream>
65 #include <climits>
66 
67 #ifdef HAVE_CONFIG_H
68  #include "config.h"
69 #endif
70 
71 namespace WFMath {
72 
73 const MTRand::uint32 MTRand::state_size;
74 
75 MTRand MTRand::instance;
76 
77 static const MTRand::uint32 period = 397;
78 static const MTRand::uint32 MATRIX_A = 0x9908b0df;
79 static const MTRand::uint32 UPPER_MASK = 0x80000000;
80 static const MTRand::uint32 LOWER_MASK = 0x7fffffff;
81 
82 
83 static MTRand::uint32 hash( time_t t, clock_t c )
84 {
85  // Get a uint32 from t and c
86  // Better than uint32(x) in case x is floating point in [0,1]
87  // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
88 
89  typedef MTRand::uint32 uint32;
90 
91  // guarantee time-based seeds will change
92  static uint32 differ = 0;
93 
94  uint32 h1 = 0;
95  auto *p = (unsigned char *) &t;
96  for( size_t i = 0; i < sizeof(t); ++i )
97  {
98  h1 *= UCHAR_MAX + 2U;
99  h1 += p[i];
100  }
101  uint32 h2 = 0;
102  p = (unsigned char *) &c;
103  for( size_t j = 0; j < sizeof(c); ++j )
104  {
105  h2 *= UCHAR_MAX + 2U;
106  h2 += p[j];
107  }
108  return ( h1 + differ++ ) ^ h2;
109 }
110 
111 
112 void MTRand::seed()
113 {
114  // First try getting an array from /dev/urandom
115  FILE* urandom = fopen( "/dev/urandom", "rb" );
116  if( urandom )
117  {
118  uint32 init_vector[state_size];
119  uint32 *s = init_vector;
120  int i = state_size;
121  bool success = true;
122  while( success && i-- )
123  success = fread( s++, sizeof(uint32), 1, urandom );
124  fclose(urandom);
125  if( success ) { seed( init_vector, state_size ); return; }
126  }
127 
128  // Was not successful, so use time() and clock() instead
129  seed( hash( time(nullptr), clock() ) );
130 }
131 
132 
133 void MTRand::seed(uint32 s)
134 {
135  state[0] = s;
136  for (index = 1; index < state_size; ++index)
137  {
138  state[index] = (1812433253UL * (state[index-1] ^ (state[index-1] >> 30u)) + index);
139  }
140 }
141 
142 
143 void MTRand::seed(const uint32 init_vector[], const uint32 init_vector_length)
144 {
145  seed(19650218UL);
146  uint32 i = 1;
147  uint32 j = 0;
148  uint32 k = (state_size > init_vector_length ? state_size : init_vector_length);
149  for( ; k; --k )
150  {
151  state[i] ^= ((state[i-1] ^ (state[i-1] >> 30u)) * 1664525UL);
152  state[i] += (init_vector[j] & 0xffffffffUL) + j;
153  ++i; ++j;
154  if (i >= state_size) { state[0] = state[state_size-1]; i = 1; }
155  if (j >= init_vector_length) j = 0;
156  }
157  for (k = state_size - 1; k; --k)
158  {
159  state[i] ^= ((state[i-1] ^ (state[i-1] >> 30u)) * 1566083941UL);
160  state[i] -= i;
161  ++i;
162  if (i >= state_size) { state[0] = state[state_size-1]; i = 1; }
163  }
164  state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array
165 }
166 
167 
168 MTRand::uint32 MTRand::randInt()
169 {
170  uint32 y;
171  static unsigned long mag01[2]={0x0UL, MATRIX_A};
172 
173  /* generate state_size words at one time */
174  if (index >= state_size)
175  {
176  uint32 kk;
177  for (kk=0; kk < state_size - period; kk++)
178  {
179  y = (state[kk]&UPPER_MASK) | (state[kk+1]&LOWER_MASK);
180  state[kk] = state[kk + period] ^ (y >> 1u) ^ mag01[y & 0x01u];
181  }
182  for (; kk < state_size-1; kk++)
183  {
184  y = (state[kk]&UPPER_MASK) | (state[kk+1]&LOWER_MASK);
185  state[kk] = state[kk+(period - state_size)] ^ (y >> 1u) ^ mag01[y & 0x01u];
186  }
187  y = (state[state_size-1]&UPPER_MASK) | (state[0]&LOWER_MASK);
188  state[state_size-1] = state[period-1] ^ (y >> 1u) ^ mag01[y & 0x1UL];
189 
190  index = 0;
191  }
192 
193  y = state[index++];
194 
195  /* Tempering */
196  y ^= (y >> 11u);
197  y ^= (y << 7u) & 0x9d2c5680UL;
198  y ^= (y << 15u) & 0xefc60000UL;
199  y ^= (y >> 18u);
200 
201  return y;
202 }
203 
204 
205 std::ostream& MTRand::save(std::ostream& ostr) const
206 {
207  for (auto i : state)
208  {
209  ostr << i << '\t';
210  }
211  return ostr << index;
212 }
213 
214 
215 std::istream& MTRand::load(std::istream& istr)
216 {
217  for (auto & i : state)
218  istr >> i;
219  istr >> index;
220  return istr;
221 }
222 
223 }
Generic library namespace.
Definition: shape.h:41