sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_test_random.c (2772B)


      1 /*
      2   Simple DirectMedia Layer
      3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
      4 
      5   This software is provided 'as-is', without any express or implied
      6   warranty.  In no event will the authors be held liable for any damages
      7   arising from the use of this software.
      8 
      9   Permission is granted to anyone to use this software for any purpose,
     10   including commercial applications, and to alter it and redistribute it
     11   freely, subject to the following restrictions:
     12 
     13   1. The origin of this software must not be misrepresented; you must not
     14      claim that you wrote the original software. If you use this software
     15      in a product, an acknowledgment in the product documentation would be
     16      appreciated but is not required.
     17   2. Altered source versions must be plainly marked as such, and must not be
     18      misrepresented as being the original software.
     19   3. This notice may not be removed or altered from any source distribution.
     20 */
     21 
     22 /*
     23 
     24  A portable "32-bit Multiply with carry" random number generator.
     25 
     26  Used by the fuzzer component.
     27  Original source code contributed by A. Schiffler for GSOC project.
     28 
     29 */
     30 
     31 #include "SDL_config.h"
     32 
     33 #include <stdlib.h>
     34 #include <stdio.h>
     35 #include <time.h>
     36 
     37 #include "SDL_test.h"
     38 
     39 /* Initialize random number generator with two integer variables */
     40 
     41 void SDLTest_RandomInit(SDLTest_RandomContext * rndContext, unsigned int xi, unsigned int ci)
     42 {
     43   if (rndContext==NULL) return;
     44 
     45   /*
     46    * Choose a value for 'a' from this list
     47    * 1791398085 1929682203 1683268614 1965537969 1675393560
     48    * 1967773755 1517746329 1447497129 1655692410 1606218150
     49    * 2051013963 1075433238 1557985959 1781943330 1893513180
     50    * 1631296680 2131995753 2083801278 1873196400 1554115554
     51    */
     52   rndContext->a = 1655692410;
     53   rndContext->x = 30903;
     54   rndContext->c = 0;
     55   if (xi != 0) {
     56       rndContext->x = xi;
     57   }
     58   rndContext->c = ci;
     59   rndContext->ah = rndContext->a >> 16;
     60   rndContext->al = rndContext->a & 65535;
     61 }
     62 
     63 /* Initialize random number generator from system time */
     64 
     65 void SDLTest_RandomInitTime(SDLTest_RandomContext * rndContext)
     66 {
     67   int a, b;
     68 
     69   if (rndContext==NULL) return;
     70 
     71   srand((unsigned int)time(NULL));
     72   a=rand();
     73   srand((unsigned int)clock());
     74   b=rand();
     75   SDLTest_RandomInit(rndContext, a, b);
     76 }
     77 
     78 /* Returns random numbers */
     79 
     80 unsigned int SDLTest_Random(SDLTest_RandomContext * rndContext)
     81 {
     82   unsigned int xh, xl;
     83 
     84   if (rndContext==NULL) return -1;
     85 
     86   xh = rndContext->x >> 16, xl = rndContext->x & 65535;
     87   rndContext->x = rndContext->x * rndContext->a + rndContext->c;
     88   rndContext->c =
     89     xh * rndContext->ah + ((xh * rndContext->al) >> 16) +
     90     ((xl * rndContext->ah) >> 16);
     91   if (xl * rndContext->al >= (~rndContext->c + 1))
     92     rndContext->c++;
     93   return (rndContext->x);
     94 }
     95 
     96 /* vi: set ts=4 sw=4 expandtab: */