SDL_test_random.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. /*
  19. A portable "32-bit Multiply with carry" random number generator.
  20. Used by the fuzzer component.
  21. Original source code contributed by A. Schiffler for GSOC project.
  22. */
  23. #include "SDL_config.h"
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <time.h>
  27. #include "SDL_test.h"
  28. /* Initialize random number generator with two integer variables */
  29. void SDLTest_RandomInit(SDLTest_RandomContext *rndContext, unsigned int xi, unsigned int ci)
  30. {
  31. if (!rndContext) {
  32. return;
  33. }
  34. /*
  35. * Choose a value for 'a' from this list
  36. * 1791398085 1929682203 1683268614 1965537969 1675393560
  37. * 1967773755 1517746329 1447497129 1655692410 1606218150
  38. * 2051013963 1075433238 1557985959 1781943330 1893513180
  39. * 1631296680 2131995753 2083801278 1873196400 1554115554
  40. */
  41. rndContext->a = 1655692410;
  42. rndContext->x = 30903;
  43. rndContext->c = 0;
  44. if (xi != 0) {
  45. rndContext->x = xi;
  46. }
  47. rndContext->c = ci;
  48. rndContext->ah = rndContext->a >> 16;
  49. rndContext->al = rndContext->a & 65535;
  50. }
  51. /* Initialize random number generator from system time */
  52. void SDLTest_RandomInitTime(SDLTest_RandomContext *rndContext)
  53. {
  54. int a, b;
  55. if (!rndContext) {
  56. return;
  57. }
  58. srand((unsigned int)time(NULL));
  59. a = rand();
  60. srand((unsigned int)SDL_GetPerformanceCounter());
  61. b = rand();
  62. SDLTest_RandomInit(rndContext, a, b);
  63. }
  64. /* Returns random numbers */
  65. unsigned int SDLTest_Random(SDLTest_RandomContext *rndContext)
  66. {
  67. unsigned int xh, xl;
  68. if (!rndContext) {
  69. return -1;
  70. }
  71. xh = rndContext->x >> 16;
  72. xl = rndContext->x & 65535;
  73. rndContext->x = rndContext->x * rndContext->a + rndContext->c;
  74. rndContext->c =
  75. xh * rndContext->ah + ((xh * rndContext->al) >> 16) +
  76. ((xl * rndContext->ah) >> 16);
  77. if (xl * rndContext->al >= (~rndContext->c + 1)) {
  78. rndContext->c++;
  79. }
  80. return rndContext->x;
  81. }
  82. /* vi: set ts=4 sw=4 expandtab: */