1
#include "mt19937.h"
2
#include "mt19937-jump.h"
3

4 1
void mt19937_seed(mt19937_state *state, uint32_t seed) {
5
  int pos;
6 1
  seed &= 0xffffffffUL;
7

8
  /* Knuth's PRNG as used in the Mersenne Twister reference implementation */
9 1
  for (pos = 0; pos < RK_STATE_LEN; pos++) {
10 1
    state->key[pos] = seed;
11 1
    seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL;
12
  }
13 1
  state->pos = RK_STATE_LEN;
14
}
15

16
/* initializes mt[RK_STATE_LEN] with a seed */
17
static void init_genrand(mt19937_state *state, uint32_t s) {
18
  int mti;
19 1
  uint32_t *mt = state->key;
20

21 1
  mt[0] = s & 0xffffffffUL;
22 1
  for (mti = 1; mti < RK_STATE_LEN; mti++) {
23
    /*
24
     * See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
25
     * In the previous versions, MSBs of the seed affect
26
     * only MSBs of the array mt[].
27
     * 2002/01/09 modified by Makoto Matsumoto
28
     */
29 1
    mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
30
    /* for > 32 bit machines */
31
    mt[mti] &= 0xffffffffUL;
32
  }
33 1
  state->pos = mti;
34
  return;
35
}
36

37
/*
38
 * initialize by an array with array-length
39
 * init_key is the array for initializing keys
40
 * key_length is its length
41
 */
42 1
void mt19937_init_by_array(mt19937_state *state, uint32_t *init_key,
43
                           int key_length) {
44
  /* was signed in the original code. RDH 12/16/2002 */
45 1
  int i = 1;
46 1
  int j = 0;
47 1
  uint32_t *mt = state->key;
48
  int k;
49

50 1
  init_genrand(state, 19650218UL);
51 1
  k = (RK_STATE_LEN > key_length ? RK_STATE_LEN : key_length);
52 1
  for (; k; k--) {
53
    /* non linear */
54 1
    mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL)) +
55 1
            init_key[j] + j;
56
    /* for > 32 bit machines */
57
    mt[i] &= 0xffffffffUL;
58 1
    i++;
59 1
    j++;
60 1
    if (i >= RK_STATE_LEN) {
61 1
      mt[0] = mt[RK_STATE_LEN - 1];
62 1
      i = 1;
63
    }
64 1
    if (j >= key_length) {
65 1
      j = 0;
66
    }
67
  }
68 1
  for (k = RK_STATE_LEN - 1; k; k--) {
69 1
    mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941UL)) -
70
            i;             /* non linear */
71
    mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
72 1
    i++;
73 1
    if (i >= RK_STATE_LEN) {
74 1
      mt[0] = mt[RK_STATE_LEN - 1];
75 1
      i = 1;
76
    }
77
  }
78

79 1
  mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
80
}
81

82 1
void mt19937_gen(mt19937_state *state) {
83
  uint32_t y;
84
  int i;
85

86 1
  for (i = 0; i < N - M; i++) {
87 1
    y = (state->key[i] & UPPER_MASK) | (state->key[i + 1] & LOWER_MASK);
88 1
    state->key[i] = state->key[i + M] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
89
  }
90 1
  for (; i < N - 1; i++) {
91 1
    y = (state->key[i] & UPPER_MASK) | (state->key[i + 1] & LOWER_MASK);
92 1
    state->key[i] = state->key[i + (M - N)] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
93
  }
94 1
  y = (state->key[N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
95 1
  state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
96

97 1
  state->pos = 0;
98
}
99

100
extern inline uint64_t mt19937_next64(mt19937_state *state);
101

102
extern inline uint32_t mt19937_next32(mt19937_state *state);
103

104
extern inline double mt19937_next_double(mt19937_state *state);
105

106 1
void mt19937_jump(mt19937_state *state) { mt19937_jump_state(state); }

Read our documentation on viewing source code .

Loading