2 * Mersenne Twister Random Algorithm
3 * Copyright (c) 2006 Ryan Martell.
4 * Based on A C-program for MT19937, with initialization improved 2002/1/26. Coded by
5 * Takuji Nishimura and Makoto Matsumoto.
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 see http://en.wikipedia.org/wiki/Mersenne_twister for an explanation of this algorithm.
37 /* Period parameters */
39 #define A 0x9908b0df /* constant vector a */
40 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
41 #define LOWER_MASK 0x7fffffff /* least significant r bits */
43 /** initializes mt[AV_RANDOM_N] with a seed */
44 void av_init_random(unsigned int seed, AVRandomState *state)
49 This differs from the wikipedia article. Source is from the Makoto
50 Makoto Matsumoto and Takuji Nishimura code, with the following comment:
52 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
53 /* In the previous versions, MSBs of the seed affect */
54 /* only MSBs of the array mt[]. */
55 state->mt[0] = seed & 0xffffffff;
56 for (index = 1; index < AV_RANDOM_N; index++) {
57 unsigned int prev= state->mt[index - 1];
58 state->mt[index] = (1812433253UL * (prev ^ (prev>>30)) + index) & 0xffffffff;
60 state->index= index; // will cause it to generate untempered numbers the first iteration
63 /** generate AV_RANDOM_N words at one time (which will then be tempered later) (av_random calls this; you shouldn't) */
64 void av_random_generate_untempered_numbers(AVRandomState *state)
69 for (kk = 0; kk < AV_RANDOM_N - M; kk++) {
70 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
71 state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ ((y&1)*A);
73 for (; kk < AV_RANDOM_N - 1; kk++) {
74 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
75 state->mt[kk] = state->mt[kk + (M - AV_RANDOM_N)] ^ (y >> 1) ^ ((y&1)*A);
77 y = (state->mt[AV_RANDOM_N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
78 state->mt[AV_RANDOM_N - 1] = state->mt[M - 1] ^ (y >> 1) ^ ((y&1)*A);
89 av_init_random(0xdeadbeef, &state);
90 for (j = 0; j < 100; j++) {
92 x+= av_random(&state);
93 STOP_TIMER("first call to av_random");
94 for (i = 1; i < AV_RANDOM_N; i++) {
96 x+= av_random(&state);
97 STOP_TIMER("AV_RANDOM_N calls of av_random");
100 av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x);