Mercurial > libavutil.hg
comparison random.c @ 239:06fd8e5c5a04 libavutil
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
author | michael |
---|---|
date | Thu, 08 Feb 2007 22:09:05 +0000 |
parents | |
children | a5c3bc3dcd3e |
comparison
equal
deleted
inserted
replaced
238:26e485add213 | 239:06fd8e5c5a04 |
---|---|
1 /* | |
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. | |
6 * | |
7 * This file is part of FFmpeg. | |
8 * | |
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. | |
13 * | |
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. | |
18 * | |
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 | |
22 */ | |
23 | |
24 | |
25 /** | |
26 see http://en.wikipedia.org/wiki/Mersenne_twister for an explanation of this algorithm. | |
27 */ | |
28 #include <stdio.h> | |
29 #include "random.h" | |
30 | |
31 //#define DEBUG | |
32 | |
33 #ifdef DEBUG | |
34 #include "common.h" | |
35 #include "log.h" | |
36 #endif | |
37 | |
38 | |
39 /* Period parameters */ | |
40 #define M 397 | |
41 #define A 0x9908b0df /* constant vector a */ | |
42 #define UPPER_MASK 0x80000000 /* most significant w-r bits */ | |
43 #define LOWER_MASK 0x7fffffff /* least significant r bits */ | |
44 | |
45 /** initializes mt[AV_RANDOM_N] with a seed */ | |
46 void av_init_random(unsigned int seed, AVRandomState *state) | |
47 { | |
48 int index; | |
49 | |
50 /* | |
51 This differs from the wikipedia article. Source is from the Makoto | |
52 Makoto Matsumoto and Takuji Nishimura code, with the following comment: | |
53 */ | |
54 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ | |
55 /* In the previous versions, MSBs of the seed affect */ | |
56 /* only MSBs of the array mt[]. */ | |
57 state->mt[0] = seed & 0xffffffff; | |
58 for (index = 1; index < AV_RANDOM_N; index++) { | |
59 unsigned int prev= state->mt[index - 1]; | |
60 state->mt[index] = (1812433253UL * (prev ^ (prev>>30)) + index) & 0xffffffff; | |
61 } | |
62 state->index= index; // will cause it to generate untempered numbers the first iteration | |
63 } | |
64 | |
65 /** generate AV_RANDOM_N words at one time (which will then be tempered later) (av_random calls this; you shouldn't) */ | |
66 void av_random_generate_untempered_numbers(AVRandomState *state) | |
67 { | |
68 int kk; | |
69 unsigned int y; | |
70 | |
71 for (kk = 0; kk < AV_RANDOM_N - M; kk++) { | |
72 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); | |
73 state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ ((y&1)*A); | |
74 } | |
75 for (; kk < AV_RANDOM_N - 1; kk++) { | |
76 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); | |
77 state->mt[kk] = state->mt[kk + (M - AV_RANDOM_N)] ^ (y >> 1) ^ ((y&1)*A); | |
78 } | |
79 y = (state->mt[AV_RANDOM_N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK); | |
80 state->mt[AV_RANDOM_N - 1] = state->mt[M - 1] ^ (y >> 1) ^ ((y&1)*A); | |
81 state->index = 0; | |
82 } | |
83 | |
84 #ifdef DEBUG | |
85 void av_benchmark_random(void) | |
86 { | |
87 int x=0; | |
88 int i, j; | |
89 AVRandomState state; | |
90 | |
91 av_init_random(0xdeadbeef, &state); | |
92 for (j = 0; j < 100; j++) { | |
93 START_TIMER; | |
94 x+= av_random(&state); | |
95 STOP_TIMER("first call to av_random"); | |
96 for (i = 1; i < AV_RANDOM_N; i++) { | |
97 START_TIMER; | |
98 x+= av_random(&state); | |
99 STOP_TIMER("AV_RANDOM_N calls of av_random"); | |
100 } | |
101 } | |
102 av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x); | |
103 } | |
104 #endif |