Debian Codegraph @

src/mtrand.c

raw symbols SCIP
1 /*
2 Copyright (C) 1997--2004, Makoto Matsumoto, Takuji Nishimura, and
3 Eric Landry; All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer
14 in the documentation and/or other materials provided with the
15 distribution.
16
17 3. The names of its contributors may not be used to endorse or
18 promote products derived from this software without specific
19 prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 Any feedback is very welcome.
34 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
35 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
36
37 Reference: M. Matsumoto and T. Nishimura, "Mersenne Twister:
38 A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number
39 Generator", ACM Transactions on Modeling and Computer Simulation,
40 Vol. 8, No. 1, January 1998, pp 3--30.
41 */
42
43 #include "mtrand.h"
44
45 /* Period parameters */
46 #define N 624
47 #define M 397
48 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
49 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
50 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
51
52 static unsigned long x[N]; /* the array for the state vector */
53 static unsigned long *p0, *p1, *pm;
54
55 void mt_srand(unsigned long s)
56 {
57 int i;
58
59 x[0] = s & 0xffffffffUL;
60 for (i = 1; i < N; ++i) {
61 x[i] = (1812433253UL * (x[i - 1] ^ (x[i - 1] >> 30)) + i)
62 & 0xffffffffUL; /* for >32 bit machines */
63 }
64 p0 = x;
65 p1 = x + 1;
66 pm = x + M;
67 }
68
69 /* generates a random number on the interval [0,0xffffffff] */
70 unsigned long mt_rand(void)
71 {
72 unsigned long y;
73
74 if (!p0) {
75 /* Default seed */
76 mt_srand(5489UL);
77 }
78 /* Twisted feedback */
79 y = *p0 = *pm++ ^ (((*p0 & UPPER_MASK) | (*p1 & LOWER_MASK)) >> 1) ^ ((~(*p1 & 1)+1) & MATRIX_A);
80 p0 = p1++;
81 if (pm == x + N) {
82 pm = x;
83 }
84 if (p1 == x + N) {
85 p1 = x;
86 }
87 /* Temper */
88 y ^= y >> 11;
89 y ^= y << 7 & 0x9d2c5680UL;
90 y ^= y << 15 & 0xefc60000UL;
91 y ^= y >> 18;
92 return y;
93 }
94
95 /* generates a random number on the interval [0,1]. */
96 float mt_rand_1(void)
97 {
98 return ((float)mt_rand() / (float)MT_RAND_MAX);
99 }
100
101 /* generates a random number on the interval [0,1). */
102 float mt_rand_lt1(void)
103 {
104 /* MT_RAND_MAX must be a float before adding one to it! */
105 return ((float)mt_rand() / ((float)MT_RAND_MAX + 1.0f));
106 }