random.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. see http://en.wikipedia.org/wiki/Mersenne_twister for an explanation of this algorithm.
  25. */
  26. #include <stdio.h>
  27. #include "random.h"
  28. /* Period parameters */
  29. #define M 397
  30. #define A 0x9908b0df /* constant vector a */
  31. #define UPPER_MASK 0x80000000 /* most significant w-r bits */
  32. #define LOWER_MASK 0x7fffffff /* least significant r bits */
  33. /** initializes mt[AV_RANDOM_N] with a seed */
  34. void av_init_random(unsigned int seed, AVRandomState *state)
  35. {
  36. int index;
  37. /*
  38. This differs from the wikipedia article. Source is from the
  39. Makoto Matsumoto and Takuji Nishimura code, with the following comment:
  40. */
  41. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  42. /* In the previous versions, MSBs of the seed affect */
  43. /* only MSBs of the array mt[]. */
  44. state->mt[0] = seed & 0xffffffff;
  45. for (index = 1; index < AV_RANDOM_N; index++) {
  46. unsigned int prev= state->mt[index - 1];
  47. state->mt[index] = (1812433253UL * (prev ^ (prev>>30)) + index) & 0xffffffff;
  48. }
  49. state->index= index; // will cause it to generate untempered numbers the first iteration
  50. }
  51. /** generate AV_RANDOM_N words at one time (which will then be tempered later) (av_random calls this; you shouldn't) */
  52. void av_random_generate_untempered_numbers(AVRandomState *state)
  53. {
  54. int kk;
  55. unsigned int y;
  56. for (kk = 0; kk < AV_RANDOM_N - M; kk++) {
  57. y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
  58. state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ ((y&1)*A);
  59. }
  60. for (; kk < AV_RANDOM_N - 1; kk++) {
  61. y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
  62. state->mt[kk] = state->mt[kk + (M - AV_RANDOM_N)] ^ (y >> 1) ^ ((y&1)*A);
  63. }
  64. y = (state->mt[AV_RANDOM_N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
  65. state->mt[AV_RANDOM_N - 1] = state->mt[M - 1] ^ (y >> 1) ^ ((y&1)*A);
  66. state->index = 0;
  67. }
  68. #ifdef TEST
  69. #include "common.h"
  70. #include "log.h"
  71. int main(void)
  72. {
  73. int x=0;
  74. int i, j;
  75. AVRandomState state;
  76. av_init_random(0xdeadbeef, &state);
  77. for (j = 0; j < 10000; j++) {
  78. START_TIMER
  79. for (i = 0; i < 624; i++) {
  80. x+= av_random(&state);
  81. }
  82. STOP_TIMER("624 calls of av_random");
  83. }
  84. av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x);
  85. return 0;
  86. }
  87. #endif