• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavutil/random.c

Go to the documentation of this file.
00001 /*
00002  * Mersenne Twister PRNG algorithm
00003  * Copyright (c) 2006 Ryan Martell
00004  * Based on a C program for MT19937, with initialization improved 2002/1/26.
00005  * Coded by Takuji Nishimura and Makoto Matsumoto.
00006  *
00007  * This file is part of FFmpeg.
00008  *
00009  * FFmpeg is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * FFmpeg is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with FFmpeg; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00022  */
00023 
00024 
00029 #include <stdio.h>
00030 #include "random.h"
00031 
00032 
00033 /* period parameters */
00034 #define M 397
00035 #define A 0x9908b0df /* constant vector a */
00036 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
00037 #define LOWER_MASK 0x7fffffff /* least significant r bits */
00038 
00040 void av_random_init(AVRandomState *state, unsigned int seed)
00041 {
00042     int index;
00043 
00044     /*
00045      This differs from the Wikipedia article.  Source is from the
00046      Makoto Matsumoto and Takuji Nishimura code, with the following comment:
00047      */
00048      /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
00049      /* In the previous versions, MSBs of the seed affect   */
00050      /* only MSBs of the array mt[].                        */
00051     state->mt[0] = seed & 0xffffffff;
00052     for (index = 1; index < AV_RANDOM_N; index++) {
00053         unsigned int prev= state->mt[index - 1];
00054         state->mt[index] = (1812433253UL * (prev ^ (prev>>30)) + index) & 0xffffffff;
00055     }
00056     state->index= index; // Will cause it to generate untempered numbers in the first iteration.
00057 }
00058 
00059 #if LIBAVUTIL_VERSION_MAJOR < 50
00060 void av_init_random(unsigned int seed, AVRandomState *state)
00061 {
00062     av_random_init(state, seed);
00063 }
00064 #endif
00065 
00068 void av_random_generate_untempered_numbers(AVRandomState *state)
00069 {
00070     int kk;
00071     unsigned int y;
00072 
00073     for (kk = 0; kk < AV_RANDOM_N - M; kk++) {
00074         y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
00075         state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ ((y&1)*A);
00076     }
00077     for (; kk < AV_RANDOM_N - 1; kk++) {
00078         y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
00079         state->mt[kk] = state->mt[kk + (M - AV_RANDOM_N)] ^ (y >> 1) ^ ((y&1)*A);
00080     }
00081     y = (state->mt[AV_RANDOM_N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
00082     state->mt[AV_RANDOM_N - 1] = state->mt[M - 1] ^ (y >> 1) ^ ((y&1)*A);
00083     state->index = 0;
00084 }
00085 
00086 #ifdef TEST
00087 #include "common.h"
00088 #include "log.h"
00089 int main(void)
00090 {
00091     int x=0;
00092     int i, j;
00093     AVRandomState state;
00094 
00095     av_random_init(&state, 0xdeadbeef);
00096     for (j = 0; j < 10000; j++) {
00097         START_TIMER
00098         for (i = 0; i < 624; i++) {
00099             x+= av_random(&state);
00100         }
00101         STOP_TIMER("624 calls of av_random");
00102     }
00103     av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x);
00104     return 0;
00105 }
00106 #endif

Generated on Sat Feb 16 2013 09:23:15 for ffmpeg by  doxygen 1.7.1