You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

70 lines
2.4 KiB

  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. #ifndef FFMPEG_RANDOM_H
  24. #define FFMPEG_RANDOM_H
  25. #define AV_RANDOM_N 624
  26. typedef struct {
  27. unsigned int mt[AV_RANDOM_N]; ///< the array for the state vector
  28. int index; ///< current untempered value we use as the base.
  29. } AVRandomState;
  30. 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
  31. void av_random_generate_untempered_numbers(AVRandomState *state); ///< Regenerate the untempered numbers (must be done every 624 iterations, or it will loop)
  32. /** generates a random number on [0,0xffffffff]-interval */
  33. __inline unsigned int av_random(AVRandomState *state)
  34. {
  35. unsigned int y;
  36. // regenerate the untempered numbers if we should...
  37. if (state->index >= AV_RANDOM_N)
  38. av_random_generate_untempered_numbers(state);
  39. // grab one...
  40. y = state->mt[state->index++];
  41. /* Now temper (Mersenne Twister coefficients) The coefficients for MT19937 are.. */
  42. y ^= (y >> 11);
  43. y ^= (y << 7) & 0x9d2c5680;
  44. y ^= (y << 15) & 0xefc60000;
  45. y ^= (y >> 18);
  46. return y;
  47. }
  48. /** return random in range [0-1] as double */
  49. __inline double av_random_real1(AVRandomState *state)
  50. {
  51. /* divided by 2^32-1 */
  52. return av_random(state) * (1.0 / 4294967296.0);
  53. }
  54. // only available if DEBUG is defined in the .c file
  55. void av_benchmark_random(void);
  56. #endif /* FFMPEG_RANDOM_H */