SDL  2.0
SDL_test_random.c File Reference
#include "SDL_config.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "SDL_test.h"
+ Include dependency graph for SDL_test_random.c:

Go to the source code of this file.

Functions

void SDLTest_RandomInit (SDLTest_RandomContext *rndContext, unsigned int xi, unsigned int ci)
 Initialize random number generator with two integers. More...
 
void SDLTest_RandomInitTime (SDLTest_RandomContext *rndContext)
 Initialize random number generator based on current system time. More...
 
unsigned int SDLTest_Random (SDLTest_RandomContext *rndContext)
 Initialize random number generator based on current system time. More...
 

Function Documentation

◆ SDLTest_Random()

unsigned int SDLTest_Random ( SDLTest_RandomContext rndContext)

Initialize random number generator based on current system time.

Note: ...RandomInit() or ...RandomInitTime() must have been called before using this function.

Parameters
rndContextpointer to context structure
Returns
A random number (32bit unsigned integer)

Definition at line 80 of file SDL_test_random.c.

References SDLTest_RandomContext::a, SDLTest_RandomContext::ah, SDLTest_RandomContext::al, SDLTest_RandomContext::c, NULL, and SDLTest_RandomContext::x.

Referenced by SDLTest_GenerateRunSeed().

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 }
#define NULL
Definition: begin_code.h:164

◆ SDLTest_RandomInit()

void SDLTest_RandomInit ( SDLTest_RandomContext rndContext,
unsigned int  xi,
unsigned int  ci 
)

Initialize random number generator with two integers.

Note: The random sequence of numbers returned by ...Random() is the same for the same two integers and has a period of 2^31.

Parameters
rndContextpointer to context structure
xiinteger that defines the random sequence
ciinteger that defines the random sequence

Definition at line 41 of file SDL_test_random.c.

References SDLTest_RandomContext::a, SDLTest_RandomContext::ah, SDLTest_RandomContext::al, SDLTest_RandomContext::c, NULL, and SDLTest_RandomContext::x.

Referenced by main(), SDLTest_FuzzerInit(), and SDLTest_RandomInitTime().

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 }
#define NULL
Definition: begin_code.h:164

◆ SDLTest_RandomInitTime()

void SDLTest_RandomInitTime ( SDLTest_RandomContext rndContext)

Initialize random number generator based on current system time.

Parameters
rndContextpointer to context structure

Definition at line 65 of file SDL_test_random.c.

References NULL, and SDLTest_RandomInit().

Referenced by main(), and SDLTest_GenerateRunSeed().

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 }
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
void SDLTest_RandomInit(SDLTest_RandomContext *rndContext, unsigned int xi, unsigned int ci)
Initialize random number generator with two integers.
#define NULL
Definition: begin_code.h:164
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b