This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
Simple DirectMedia Layer | ||
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org> | ||
This software is provided 'as-is', without any express or implied | ||
warranty. In no event will the authors be held liable for any damages | ||
arising from the use of this software. | ||
Permission is granted to anyone to use this software for any purpose, | ||
including commercial applications, and to alter it and redistribute it | ||
freely, subject to the following restrictions: | ||
1. The origin of this software must not be misrepresented; you must not | ||
claim that you wrote the original software. If you use this software | ||
in a product, an acknowledgment in the product documentation would be | ||
appreciated but is not required. | ||
2. Altered source versions must be plainly marked as such, and must not be | ||
misrepresented as being the original software. | ||
3. This notice may not be removed or altered from any source distribution. | ||
*/ | ||
|
||
/** | ||
* \file SDL_test_random.h | ||
* | ||
* Include file for SDL test framework. | ||
* | ||
* This code is a part of the SDL2_test library, not the main SDL library. | ||
*/ | ||
|
||
/* | ||
A "32-bit Multiply with carry random number generator. Very fast. | ||
Includes a list of recommended multipliers. | ||
multiply-with-carry generator: x(n) = a*x(n-1) + carry mod 2^32. | ||
period: (a*2^31)-1 | ||
*/ | ||
|
||
#ifndef _SDL_test_random_h | ||
#define _SDL_test_random_h | ||
|
||
#include "begin_code.h" | ||
/* Set up for C function definitions, even when using C++ */ | ||
#ifdef __cplusplus | ||
/* *INDENT-OFF* */ | ||
extern "C" { | ||
/* *INDENT-ON* */ | ||
#endif | ||
|
||
/* Function prototypes */ | ||
|
||
|
||
/* ------- Definitions ------- */ | ||
|
||
/* | ||
* Macros that return random number in a specific format. | ||
* Float values are in the range [0.0-1.0]. | ||
*/ | ||
#define SDL_TestRandomInt(c) ((int)SDL_TestRandom(c)) | ||
#define SDL_TestRandomFloat(c) ((double)SDL_TestRandom(c)/(unsigned long)0xffffffff) | ||
|
||
/* | ||
* Context structure for the random number generator state. | ||
*/ | ||
typedef struct { | ||
unsigned int a; | ||
unsigned int x; | ||
unsigned int c; | ||
unsigned int ah; | ||
unsigned int al; | ||
} SDLTest_RandomContext; | ||
|
||
/* ---- Function Prototypes -------- */ | ||
|
||
/** | ||
* \brief 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. | ||
* | ||
* \param rndContext pointer to context structure | ||
* \param xi integer that defines the random sequence | ||
* \param ci integer that defines the random sequence | ||
* | ||
*/ | ||
void SDLTest_RandomInit(SDLTest_RandomContext * rndContext, unsigned int xi, | ||
unsigned int ci); | ||
|
||
/** | ||
* \brief Initialize random number generator based on current system time. | ||
* | ||
* \param rndContext pointer to context structure | ||
* | ||
*/ | ||
void SDLTest_RandomInitTime(SDLTest_RandomContext *rndContext); | ||
|
||
|
||
/** | ||
* \brief Initialize random number generator based on current system time. | ||
* | ||
* Note: ...RandomInit() or ...RandomInitTime() must have been called | ||
* before using this function. | ||
* | ||
* \param rndContext pointer to context structure | ||
* | ||
* \returns A random number (32bit unsigned integer) | ||
* | ||
*/ | ||
unsigned int SDLTest_Random(SDLTest_RandomContext *rndContext); | ||
|
||
|
||
/* Ends C function definitions when using C++ */ | ||
#ifdef __cplusplus | ||
/* *INDENT-OFF* */ | ||
} | ||
/* *INDENT-ON* */ | ||
#endif | ||
#include "close_code.h" | ||
|
||
#endif /* _SDL_test_random_h */ | ||
|
||
/* vi: set ts=4 sw=4 expandtab: */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
Simple DirectMedia Layer | ||
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org> | ||
This software is provided 'as-is', without any express or implied | ||
warranty. In no event will the authors be held liable for any damages | ||
arising from the use of this software. | ||
Permission is granted to anyone to use this software for any purpose, | ||
including commercial applications, and to alter it and redistribute it | ||
freely, subject to the following restrictions: | ||
1. The origin of this software must not be misrepresented; you must not | ||
claim that you wrote the original software. If you use this software | ||
in a product, an acknowledgment in the product documentation would be | ||
appreciated but is not required. | ||
2. Altered source versions must be plainly marked as such, and must not be | ||
misrepresented as being the original software. | ||
3. This notice may not be removed or altered from any source distribution. | ||
*/ | ||
|
||
/* | ||
A portable "32-bit Multiply with carry" random number generator. | ||
Used by the fuzzer component. | ||
Original source code contributed by A. Schiffler for GSOC project. | ||
*/ | ||
|
||
#include "SDL_config.h" | ||
|
||
#include <time.h> | ||
|
||
#include "SDL_test.h" | ||
|
||
/* Initialize random number generator with two integer variables */ | ||
|
||
void SDLTest_RandomInit(SDLTest_RandomContext * rndContext, unsigned int xi, unsigned int ci) | ||
{ | ||
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; | ||
} | ||
|
||
/* Initialize random number generator from system time */ | ||
|
||
void SDLTest_RandomInitTime(SDLTest_RandomContext * rndContext) | ||
{ | ||
int a, b; | ||
|
||
if (rndContext==NULL) return; | ||
|
||
srand((unsigned int)time(NULL)); | ||
a=rand(); | ||
srand(clock()); | ||
b=rand(); | ||
SDLTest_RandomInit(rndContext, a, b); | ||
} | ||
|
||
/* Returns random numbers */ | ||
|
||
unsigned int SDLTest_Random(SDLTest_RandomContext * rndContext) | ||
{ | ||
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); | ||
} |