Register myself as Smacker maintainer
[ffmpeg-lucabe.git] / libavutil / random.c
blob94671556fcb68e03fa19f3565ff3c8983ae0aa88
1 /*
2 * Mersenne Twister PRNG algorithm
3 * Copyright (c) 2006 Ryan Martell
4 * Based on a C program for MT19937, with initialization improved 2002/1/26.
5 * Coded by 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
25 /**
26 * See http://en.wikipedia.org/wiki/Mersenne_twister
27 * for an explanation of this algorithm.
29 #include <stdio.h>
30 #include "random.h"
33 /* period parameters */
34 #define M 397
35 #define A 0x9908b0df /* constant vector a */
36 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
37 #define LOWER_MASK 0x7fffffff /* least significant r bits */
39 /** Initializes mt[AV_RANDOM_N] with a seed. */
40 void av_random_init(AVRandomState *state, unsigned int seed)
42 int index;
45 This differs from the Wikipedia article. Source is from the
46 Makoto Matsumoto and Takuji Nishimura code, with the following comment:
48 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
49 /* In the previous versions, MSBs of the seed affect */
50 /* only MSBs of the array mt[]. */
51 state->mt[0] = seed & 0xffffffff;
52 for (index = 1; index < AV_RANDOM_N; index++) {
53 unsigned int prev= state->mt[index - 1];
54 state->mt[index] = (1812433253UL * (prev ^ (prev>>30)) + index) & 0xffffffff;
56 state->index= index; // Will cause it to generate untempered numbers in the first iteration.
59 /** Generates AV_RANDOM_N words at one time (which will then be tempered later).
60 * av_random calls this; you shouldn't. */
61 void av_random_generate_untempered_numbers(AVRandomState *state)
63 int kk;
64 unsigned int y;
66 for (kk = 0; kk < AV_RANDOM_N - M; kk++) {
67 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
68 state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ ((y&1)*A);
70 for (; kk < AV_RANDOM_N - 1; kk++) {
71 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
72 state->mt[kk] = state->mt[kk + (M - AV_RANDOM_N)] ^ (y >> 1) ^ ((y&1)*A);
74 y = (state->mt[AV_RANDOM_N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
75 state->mt[AV_RANDOM_N - 1] = state->mt[M - 1] ^ (y >> 1) ^ ((y&1)*A);
76 state->index = 0;
79 #ifdef TEST
80 #include "common.h"
81 #include "log.h"
82 int main(void)
84 int x=0;
85 int i, j;
86 AVRandomState state;
88 av_random_init(&state, 0xdeadbeef);
89 for (j = 0; j < 10000; j++) {
90 START_TIMER
91 for (i = 0; i < 624; i++) {
92 x+= av_random(&state);
94 STOP_TIMER("624 calls of av_random");
96 av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x);
97 return 0;
99 #endif