random.h

Go to the documentation of this file.
00001 /*
00002  * Mersenne Twister Random Algorithm
00003  * Copyright (c) 2006 Ryan Martell.
00004  * Based on A C-program for MT19937, with initialization improved 2002/1/26. Coded by
00005  * 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 FFMPEG_RANDOM_H
00025 #define FFMPEG_RANDOM_H
00026 
00027 #define AV_RANDOM_N 624
00028 
00029 typedef struct {
00030     unsigned int mt[AV_RANDOM_N]; ///< the array for the state vector
00031     int index; ///< current untempered value we use as the base.
00032 } AVRandomState;
00033 
00034 
00035 void av_init_random(unsigned int seed, AVRandomState *state); ///< to be inlined, the struct must be visible, so it doesn't make sense to try and keep it opaque with malloc/free like calls
00036 void av_random_generate_untempered_numbers(AVRandomState *state); ///< Regenerate the untempered numbers (must be done every 624 iterations, or it will loop)
00037 
00038 /** generates a random number on [0,0xffffffff]-interval */
00039 static inline unsigned int av_random(AVRandomState *state)
00040 {
00041     unsigned int y;
00042 
00043     // regenerate the untempered numbers if we should...
00044     if (state->index >= AV_RANDOM_N)
00045         av_random_generate_untempered_numbers(state);
00046 
00047     // grab one...
00048     y = state->mt[state->index++];
00049 
00050     /* Now temper (Mersenne Twister coefficients) The coefficients for MT19937 are.. */
00051     y ^= (y >> 11);
00052     y ^= (y << 7) & 0x9d2c5680;
00053     y ^= (y << 15) & 0xefc60000;
00054     y ^= (y >> 18);
00055 
00056     return y;
00057 }
00058 
00059 /** return random in range [0-1] as double */
00060 static inline double av_random_real1(AVRandomState *state)
00061 {
00062     /* divided by 2^32-1 */
00063     return av_random(state) * (1.0 / 4294967296.0);
00064 }
00065 
00066 // only available if DEBUG is defined in the .c file
00067 void av_benchmark_random(void);
00068 
00069 #endif /* FFMPEG_RANDOM_H */

Generated on Thu Nov 20 06:44:53 2008 for libextractor by  doxygen 1.5.1