2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Copyright (C) 1997-2004, Makoto Matsumoto, Takuji Nishimura, and
6 * Eric Landry; All rights reserved. (3-clause BSD license)
7 * Daniel Borkmann: Refactored, added initialization functions.
8 * Subject to the GPL, version 2.
9 * Reference: M. Matsumoto and T. Nishimura, "Mersenne Twister:
10 * A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number
11 * Generator", ACM Transactions on Modeling and Computer Simulation,
12 * Vol. 8, No. 1, January 1998, pp 3--30.
28 #define MATRIX_A 0x9908b0dfUL
29 #define UPPER_MASK 0x80000000UL
30 #define LOWER_MASK 0x7fffffffUL
32 static unsigned long x
[N
];
33 static unsigned long *p0
, *p1
, *pm
;
35 void mt_init_by_seed_rand(unsigned long s
)
38 x
[0] = s
& 0xffffffffUL
;
39 for (i
= 1; i
< N
; ++i
) {
40 x
[i
] = (1812433253UL * (x
[i
- 1] ^ (x
[i
- 1] >> 30)) + i
) &
48 void mt_init_by_seed_time(void)
51 x
[0] = ((unsigned long) time(NULL
)) & 0xffffffffUL
;
52 for (i
= 1; i
< N
; ++i
) {
53 x
[i
] = (1812433253UL * (x
[i
- 1] ^ (x
[i
- 1] >> 30)) + i
) &
61 void mt_init_by_seed_array(unsigned long key
[], int len
)
64 mt_init_by_seed_rand(19650218UL);
67 for (k
= (N
> len
? N
: len
); k
; --k
) {
69 x
[i
] = ((x
[i
] ^ ((x
[i
- 1] ^ (x
[i
- 1] >> 30)) *
70 1664525UL)) + key
[j
] + j
) & 0xffffffffUL
;
78 for (k
= N
- 1; k
; --k
) {
80 x
[i
] = ((x
[i
] ^ ((x
[i
- 1] ^ (x
[i
- 1] >> 30)) *
81 1566083941UL)) - i
) & 0xffffffffUL
;
90 void mt_init_by_seed_rand_array(void)
93 unsigned long k
[LEN_INIT
];
94 srand((unsigned int) time(NULL
));
95 for (i
= 0; i
< LEN_INIT
; i
++)
97 mt_init_by_seed_array(k
, LEN_INIT
);
100 void mt_init_by_random_device(void)
103 unsigned long k
[LEN_INIT
];
104 fd
= open_or_die("/dev/random", O_RDONLY
);
105 read_or_die(fd
, k
, sizeof(unsigned long) * LEN_INIT
);
107 mt_init_by_seed_array(k
, LEN_INIT
);
110 unsigned long mt_rand_int32(void)
112 /* Interval [0,0xffffffff] */
116 mt_init_by_seed_rand(5489UL);
117 /* Twisted feedback */
118 y
= *p0
= *pm
++ ^ (((*p0
& UPPER_MASK
) | (*p1
& LOWER_MASK
)) >> 1) ^
119 (-(*p1
& 1) & MATRIX_A
);
127 y
^= y
<< 7 & 0x9d2c5680UL
;
128 y
^= y
<< 15 & 0xefc60000UL
;
133 long mt_rand_int31(void)
135 /* Interval [0,0x7fffffff] */
136 return (long) mt_rand_int32() >> 1;
139 double mt_rand_real1(void)
141 /* Interval [0,1]; Divided by 2^32-1 */
142 return mt_rand_int32() * (1.0 / 4294967295.0);
145 double mt_rand_real2(void)
147 /* Interval [0,1); Divided by 2^32 */
148 return mt_rand_int32() * (1.0 / 4294967296.0);
151 double mt_rand_real3(void)
153 /* Interval (0,1); Divided by 2^32 */
154 return (((double) mt_rand_int32()) + 0.5) * (1.0 / 4294967296.0);
157 double mt_rand_res53(void)
159 /* 53-bit random number on the real interval [0,1) */
160 unsigned long a
= mt_rand_int32() >> 5, b
= mt_rand_int32() >> 6;
161 return (a
* 67108864.0 + b
) * (1.0 / 9007199254740992.0);