_mt.c

filter: Statementstmt 100% 52 / 52 Branchbran 87.5% 21 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 96.0% 73 / 76 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« _stats.c ↑ Summary
1 #include "mt.h"
2  
3 #include <stdlib.h>
4 #include <stdio.h>
5  
6 /* This code is based on mt19937ar.c, written by Takuji Nishimura and
7 Makoto Matsumoto (20020126). Further details are available at
8 <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html>.
9  
10 REFERENCE
11 M. Matsumoto and T. Nishimura,
12 "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
13 Pseudo-Random Number Generator",
14 ACM Transactions on Modeling and Computer Simulation,
15 Vol. 8, No. 1, January 1998, pp 3--30. */
16  
17 4 void mt_init_seed( struct mt *m, uint32_t seed )
18 {
19 int i;
20 uint32_t *mt;
21  
22 4 mt = m->mt;
23 4 mt[0] = seed & 0xffffffff;
24 2496 ▶ for ( i = 1; i < N; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < N; i++ ) 2492 4 2496
25 2492 mt[i] = 1812433253 * (mt[i-1]^(mt[i-1]>>30)) + i;
26 4 m->mti = N;
27 4 }
28  
29 3 struct mt *mt_setup(uint32_t seed)
30 {
31 3 struct mt *self = malloc(sizeof(struct mt));
32  
33 3 ▶ if (self)
Branch: 1 branch
BranchTrueFalseTotal
if (self) 3 0 3
34 3 mt_init_seed( self, seed );
35  
36 3 return self;
37 }
38  
39 1 struct mt *mt_setup_array( uint32_t *array, int n )
40 {
41 int i, j, k;
42 1 struct mt *self = malloc(sizeof(struct mt));
43 uint32_t *mt;
44  
45 1 ▶ if (self) {
Branch: 1 branch
BranchTrueFalseTotal
if (self) { 1 0 1
46 1 mt_init_seed( self, 19650218UL );
47  
48 1 i = 1; j = 0;
49 1 k = ( N > n ? N : n );
50 1 mt = self->mt;
51  
52 625 ▶ for (; k; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (; k; k--) { 624 1 625
53 624 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
54 624 + array[j] + j;
55 624 i++; j++;
56 624 ▶ if (i>=N) { mt[0] = mt[N-1]; i=1; }
Branch: 1 branch
BranchTrueFalseTotal
if (i>=N) { mt[0] = mt[N-1]; i=1; } 1 623 624
57 624 ▶ if (j>=n) j=0;
Branch: 1 branch
BranchTrueFalseTotal
if (j>=n) j=0; 124 500 624
58 }
59 624 ▶ for (k=N-1; k; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (k=N-1; k; k--) { 623 1 624
60 623 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - i;
61 623 i++;
62 623 ▶ if (i>=N) { mt[0] = mt[N-1]; i=1; }
Branch: 1 branch
BranchTrueFalseTotal
if (i>=N) { mt[0] = mt[N-1]; i=1; } 1 622 623
63 }
64  
65 1 mt[0] = 0x80000000UL;
66 }
67  
68 1 return self;
69 }
70  
71 4 void mt_free(struct mt *self)
72 {
73 4 ▶ if (self)
Branch: 1 branch
BranchTrueFalseTotal
if (self) 4 0 4
74 4 free(self);
75 4 }
76  
77 /* Returns a pseudorandom number which is uniformly distributed in [0,1) */
78 1672 double mt_genrand(struct mt *self)
79 {
80 int kk;
81 uint32_t y;
82 static uint32_t mag01[2] = {0x0, 0x9908b0df};
83 static const uint32_t UP_MASK = 0x80000000, LOW_MASK = 0x7fffffff;
84  
85 1672 ▶ if (self->mti >= N) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->mti >= N) { 5 1667 1672
86 1140 ▶ for (kk = 0; kk < N-M; kk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (kk = 0; kk < N-M; kk++) { 1135 5 1140
87 1135 y = (self->mt[kk] & UP_MASK) | (self->mt[kk+1] & LOW_MASK);
88 1135 self->mt[kk] = self->mt[kk+M] ^ (y >> 1) ^ mag01[y & 1];
89 }
90  
91 1985 ▶ for (; kk < N-1; kk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; kk < N-1; kk++) { 1980 5 1985
92 1980 y = (self->mt[kk] & UP_MASK) | (self->mt[kk+1] & LOW_MASK);
93 1980 self->mt[kk] = self->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 1];
94 }
95  
96 5 y = (self->mt[N-1] & UP_MASK) | (self->mt[0] & LOW_MASK);
97 5 self->mt[N-1] = self->mt[M-1] ^ (y >> 1) ^ mag01[y & 1];
98  
99 5 self->mti = 0;
100 }
101
102 1672 y = self->mt[self->mti++];
103 1672 y ^= y >> 11;
104 1672 y ^= y << 7 & 0x9d2c5680;
105 1672 y ^= y << 15 & 0xefc60000;
106 1672 y ^= y >> 18;
107  
108 1672 return y*(1.0/4294967296.0);
109 }
« _stats.c ↑ Summary