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 
66 #ifdef HAVE_CONFIG_H
67  #include "config.h"
68 #endif
69 
70 namespace WFMath {
71 
72 const MTRand::uint32 MTRand::state_size;
73 
74 MTRand MTRand::instance;
75 
76 static const MTRand::uint32 period = 397;
77 static const MTRand::uint32 MATRIX_A = 0x9908b0df;
78 static const MTRand::uint32 UPPER_MASK = 0x80000000;
79 static const MTRand::uint32 LOWER_MASK = 0x7fffffff;
80 
81 
82 static MTRand::uint32 hash( time_t t, clock_t c )
83 {
84  // Get a uint32 from t and c
85  // Better than uint32(x) in case x is floating point in [0,1]
86  // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
87 
88  typedef MTRand::uint32 uint32;
89 
90  // guarantee time-based seeds will change
91  static uint32 differ = 0;
92 
93  uint32 h1 = 0;
94  auto *p = (unsigned char *) &t;
95  for( size_t i = 0; i < sizeof(t); ++i )
96  {
97  h1 *= UCHAR_MAX + 2U;
98  h1 += p[i];
99  }
100  uint32 h2 = 0;
101  p = (unsigned char *) &c;
102  for( size_t j = 0; j < sizeof(c); ++j )
103  {
104  h2 *= UCHAR_MAX + 2U;
105  h2 += p[j];
106  }
107  return ( h1 + differ++ ) ^ h2;
108 }
109 
110 
111 void MTRand::seed()
112 {
113  // First try getting an array from /dev/urandom
114  FILE* urandom = fopen( "/dev/urandom", "rb" );
115  if( urandom )
116  {
117  uint32 init_vector[state_size];
118  uint32 *s = init_vector;
119  int i = state_size;
120  bool success = true;
121  while( success && i-- )
122  success = fread( s++, sizeof(uint32), 1, urandom );
123  fclose(urandom);
124  if( success ) { seed( init_vector, state_size ); return; }
125  }
126 
127  // Was not successful, so use time() and clock() instead
128  seed( hash( time(nullptr), clock() ) );
129 }
130 
131 
132 void MTRand::seed(uint32 s)
133 {
134  state[0] = s;
135  for (index = 1; index < state_size; ++index)
136  {
137  state[index] = (1812433253UL * (state[index-1] ^ (state[index-1] >> 30u)) + index);
138  }
139 }
140 
141 
142 void MTRand::seed(const uint32 init_vector[], const uint32 init_vector_length)
143 {
144  seed(19650218UL);
145  uint32 i = 1;
146  uint32 j = 0;
147  uint32 k = (state_size > init_vector_length ? state_size : init_vector_length);
148  for( ; k; --k )
149  {
150  state[i] ^= ((state[i-1] ^ (state[i-1] >> 30u)) * 1664525UL);
151  state[i] += (init_vector[j] & 0xffffffffUL) + j;
152  ++i; ++j;
153  if (i >= state_size) { state[0] = state[state_size-1]; i = 1; }
154  if (j >= init_vector_length) j = 0;
155  }
156  for (k = state_size - 1; k; --k)
157  {
158  state[i] ^= ((state[i-1] ^ (state[i-1] >> 30u)) * 1566083941UL);
159  state[i] -= i;
160  ++i;
161  if (i >= state_size) { state[0] = state[state_size-1]; i = 1; }
162  }
163  state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array
164 }
165 
166 
167 MTRand::uint32 MTRand::randInt()
168 {
169  uint32 y;
170  static unsigned long mag01[2]={0x0UL, MATRIX_A};
171 
172  /* generate state_size words at one time */
173  if (index >= state_size)
174  {
175  uint32 kk;
176  for (kk=0; kk < state_size - period; kk++)
177  {
178  y = (state[kk]&UPPER_MASK) | (state[kk+1]&LOWER_MASK);
179  state[kk] = state[kk + period] ^ (y >> 1u) ^ mag01[y & 0x01u];
180  }
181  for (; kk < state_size-1; kk++)
182  {
183  y = (state[kk]&UPPER_MASK) | (state[kk+1]&LOWER_MASK);
184  state[kk] = state[kk+(period - state_size)] ^ (y >> 1u) ^ mag01[y & 0x01u];
185  }
186  y = (state[state_size-1]&UPPER_MASK) | (state[0]&LOWER_MASK);
187  state[state_size-1] = state[period-1] ^ (y >> 1u) ^ mag01[y & 0x1UL];
188 
189  index = 0;
190  }
191 
192  y = state[index++];
193 
194  /* Tempering */
195  y ^= (y >> 11u);
196  y ^= (y << 7u) & 0x9d2c5680UL;
197  y ^= (y << 15u) & 0xefc60000UL;
198  y ^= (y >> 18u);
199 
200  return y;
201 }
202 
203 
204 std::ostream& MTRand::save(std::ostream& ostr) const
205 {
206  for (auto i : state)
207  {
208  ostr << i << '\t';
209  }
210  return ostr << index;
211 }
212 
213 
214 std::istream& MTRand::load(std::istream& istr)
215 {
216  for (auto & i : state)
217  istr >> i;
218  istr >> index;
219  return istr;
220 }
221 
222 }
WFMath
Generic library namespace.
Definition: shape.h:41