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 #ifndef AVUTIL_RANDOM_H 00025 #define AVUTIL_RANDOM_H 00026 00027 #define AV_RANDOM_N 624 00028 00029 #include "avutil.h" 00030 #include "common.h" 00031 00032 typedef struct { 00033 unsigned int mt[AV_RANDOM_N]; 00034 int index; 00035 } AVRandomState; 00036 00037 00038 #if LIBAVUTIL_VERSION_MAJOR < 50 00039 attribute_deprecated void av_init_random(unsigned int seed, AVRandomState *state); 00040 #endif 00041 void av_random_init(AVRandomState *state, unsigned int seed); 00042 void av_random_generate_untempered_numbers(AVRandomState *state); 00043 00052 static inline unsigned int av_random(AVRandomState *state) 00053 { 00054 unsigned int y; 00055 00056 // Regenerate the untempered numbers if we should... 00057 if (state->index >= AV_RANDOM_N) 00058 av_random_generate_untempered_numbers(state); 00059 00060 // Grab one... 00061 y = state->mt[state->index++]; 00062 00063 /* Now temper (Mersenne Twister coefficients). The coefficients for MT19937 are.. */ 00064 y ^= (y >> 11); 00065 y ^= (y << 7) & 0x9d2c5680; 00066 y ^= (y << 15) & 0xefc60000; 00067 y ^= (y >> 18); 00068 00069 return y; 00070 } 00071 00073 static inline double av_random_real1(AVRandomState *state) 00074 { 00075 /* divided by 2^32-1 */ 00076 return av_random(state) * (1.0 / 4294967296.0); 00077 } 00078 00079 #endif /* AVUTIL_RANDOM_H */