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.
void SDLTest_RandomInitTime (SDLTest_RandomContext *rndContext)
 Initialize random number generator based on current system time.
unsigned int SDLTest_Random (SDLTest_RandomContext *rndContext)
 Initialize random number generator based on current system time.

Function Documentation

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().

{
unsigned int xh, xl;
if (rndContext==NULL) return -1;
xh = rndContext->x >> 16, xl = rndContext->x & 65535;
rndContext->x = rndContext->x * rndContext->a + rndContext->c;
rndContext->c =
xh * rndContext->ah + ((xh * rndContext->al) >> 16) +
((xl * rndContext->ah) >> 16);
if (xl * rndContext->al >= (~rndContext->c + 1))
rndContext->c++;
return (rndContext->x);
}
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().

{
if (rndContext==NULL) return;
/*
* Choose a value for 'a' from this list
* 1791398085 1929682203 1683268614 1965537969 1675393560
* 1967773755 1517746329 1447497129 1655692410 1606218150
* 2051013963 1075433238 1557985959 1781943330 1893513180
* 1631296680 2131995753 2083801278 1873196400 1554115554
*/
rndContext->a = 1655692410;
rndContext->x = 30903;
rndContext->c = 0;
if (xi != 0) {
rndContext->x = xi;
}
rndContext->c = ci;
rndContext->ah = rndContext->a >> 16;
rndContext->al = rndContext->a & 65535;
}
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().

{
int a, b;
if (rndContext==NULL) return;
srand((unsigned int)time(NULL));
a=rand();
srand((unsigned int)clock());
b=rand();
SDLTest_RandomInit(rndContext, a, b);
}