SDL_random.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. // This file contains portable random functions for SDL
  20. static Uint64 SDL_rand_state;
  21. static bool SDL_rand_initialized = false;
  22. void SDL_srand(Uint64 seed)
  23. {
  24. if (!seed) {
  25. seed = SDL_GetPerformanceCounter();
  26. }
  27. SDL_rand_state = seed;
  28. SDL_rand_initialized = true;
  29. }
  30. Sint32 SDL_rand(Sint32 n)
  31. {
  32. if (!SDL_rand_initialized) {
  33. SDL_srand(0);
  34. }
  35. return SDL_rand_r(&SDL_rand_state, n);
  36. }
  37. float SDL_randf(void)
  38. {
  39. if (!SDL_rand_initialized) {
  40. SDL_srand(0);
  41. }
  42. return SDL_randf_r(&SDL_rand_state);
  43. }
  44. Uint32 SDL_rand_bits(void)
  45. {
  46. if (!SDL_rand_initialized) {
  47. SDL_srand(0);
  48. }
  49. return SDL_rand_bits_r(&SDL_rand_state);
  50. }
  51. Uint32 SDL_rand_bits_r(Uint64 *state)
  52. {
  53. if (!state) {
  54. return 0;
  55. }
  56. // The C and A parameters of this LCG have been chosen based on hundreds
  57. // of core-hours of testing with PractRand and TestU01's Crush.
  58. // Using a 32-bit A improves performance on 32-bit architectures.
  59. // C can be any odd number, but < 256 generates smaller code on ARM32
  60. // These values perform as well as a full 64-bit implementation against
  61. // Crush and PractRand. Plus, their worst-case performance is better
  62. // than common 64-bit constants when tested against PractRand using seeds
  63. // with only a single bit set.
  64. // We tested all 32-bit and 33-bit A with all C < 256 from a v2 of:
  65. // Steele GL, Vigna S. Computationally easy, spectrally good multipliers
  66. // for congruential pseudorandom number generators.
  67. // Softw Pract Exper. 2022;52(2):443-458. doi: 10.1002/spe.3030
  68. // https://arxiv.org/abs/2001.05304v2
  69. *state = *state * 0xff1cd035ul + 0x05;
  70. // Only return top 32 bits because they have a longer period
  71. return (Uint32)(*state >> 32);
  72. }
  73. Sint32 SDL_rand_r(Uint64 *state, Sint32 n)
  74. {
  75. // Algorithm: get 32 bits from SDL_rand_bits() and treat it as a 0.32 bit
  76. // fixed point number. Multiply by the 31.0 bit n to get a 31.32 bit
  77. // result. Shift right by 32 to get the 31 bit integer that we want.
  78. if (n < 0) {
  79. // The algorithm looks like it works for numbers < 0 but it has an
  80. // infinitesimal chance of returning a value out of range.
  81. // Returning -SDL_rand(abs(n)) blows up at INT_MIN instead.
  82. // It's easier to just say no.
  83. return 0;
  84. }
  85. // On 32-bit arch, the compiler will optimize to a single 32-bit multiply
  86. Uint64 val = (Uint64)SDL_rand_bits_r(state) * n;
  87. return (Sint32)(val >> 32);
  88. }
  89. float SDL_randf_r(Uint64 *state)
  90. {
  91. // Note: its using 24 bits because float has 23 bits significand + 1 implicit bit
  92. return (SDL_rand_bits_r(state) >> (32 - 24)) * 0x1p-24f;
  93. }