random.c 3.4 KB

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