HILA
Loading...
Searching...
No Matches
mersenne_inline_new.cpp
1/* A C-program for MT19937: Real number version (1998/4/6) */
2/* genrand() generates one pseudorandom real number (double) */
3/* which is uniformly distributed on [0,1]-interval, for each */
4/* call. sgenrand(seed) set initial values to the working area */
5/* of 624 words. Before genrand(), sgenrand(seed) must be */
6/* called once. (seed is any 32-bit integer except for 0). */
7/* Integer generator is obtained by modifying two lines. */
8/* Coded by Takuji Nishimura, considering the suggestions by */
9/* Topher Cooper and Marc Rieffel in July-Aug. 1997. */
10
11/* This library is free software; you can redistribute it and/or */
12/* modify it under the terms of the GNU Library General Public */
13/* License as published by the Free Software Foundation; either */
14/* version 2 of the License, or (at your option) any later */
15/* version. */
16/* This library is distributed in the hope that it will be useful, */
17/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
19/* See the GNU Library General Public License for more details. */
20/* You should have received a copy of the GNU Library General */
21/* Public License along with this library; if not, write to the */
22/* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */
23/* 02111-1307 USA */
24
25/* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. */
26/* When you use this, send an email to: matumoto@math.keio.ac.jp */
27/* with an appropriate reference to your work. */
28
29/* REFERENCE */
30/* M. Matsumoto and T. Nishimura, */
31/* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */
32/* Pseudo-Random Number Generator", */
33/* ACM Transactions on Modeling and Computer Simulation, */
34/* Vol. 8, No. 1, January 1998, pp 3--30. */
35
36#include <stdlib.h>
37#include <stdio.h>
38
39#include "mersenne.h"
40
41/* Period parameters */
42#define N 624
43#define M 397
44#define MATRIX_A 0x9908b0df /* constant vector a */
45#define UPPER_MASK 0x80000000 /* most significant w-r bits */
46#define LOWER_MASK 0x7fffffff /* least significant r bits */
47
48/* Tempering parameters */
49#define TEMPERING_MASK_B 0x9d2c5680
50#define TEMPERING_MASK_C 0xefc60000
51#define TEMPERING_SHIFT_U(y) (y >> 11)
52#define TEMPERING_SHIFT_S(y) (y << 7)
53#define TEMPERING_SHIFT_T(y) (y << 15)
54#define TEMPERING_SHIFT_L(y) (y >> 18)
55
56static unsigned int mt[N]; /* the array for the state vector */
57static int mersenne_i = -1; /* < 0 means mt[N] is not initialized */
58
59/* initializing the array with a NONZERO seed */
60void seed_mersenne(long seed) {
61 int mti;
62 /* setting initial seeds to mt[N] using */
63 /* the generator Line 25 of Table 1 in */
64 /* [KNUTH 1981, The Art of Computer Programming */
65 /* Vol. 2 (2nd Ed.), pp102] */
66 if (seed % 2 == 0)
67 seed++;
68 mt[0] = seed & 0xffffffff;
69 for (mti = 1; mti < N; mti++)
70 mt[mti] = (69069 * mt[mti - 1]) & 0xffffffff;
71
72 mersenne_i = 0;
73}
74
75void /* generating reals */
76/* unsigned int */ /* for integer generation */
77mersenne_generate() {
78 unsigned int y;
79 int kk;
80 static unsigned int mag01[2] = {0x0, MATRIX_A};
81 /* mag01[x] = x * MATRIX_A for x=0,1 */
82
83 if (mersenne_i < 0) { /* if sgenrand() has not been called, */
84 printf("MERSENNE: you did not seed the generator!\n");
85 exit(1);
86 }
87
88 /* generate N words at one time */
89
90 for (kk = 0; kk < N - M; kk++) {
91 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
92 mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1];
93 }
94 for (; kk < N - 1; kk++) {
95 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
96 mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1];
97 }
98 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
99 mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1];
100}
101
102double mersenne() {
103 if (mersenne_i >= N) {
104 mersenne_generate();
105 mersenne_i = 0;
106 }
107 unsigned int y = mt[mersenne_i++];
108 y ^= TEMPERING_SHIFT_U(y);
109 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
110 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
111 y ^= TEMPERING_SHIFT_L(y);
112 return (double)y * 2.3283064365386963e-10; /* reals: interval [0,1) */
113}