Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
94 lines (73 loc) · 2.66 KB

SDL_test_random.c

File metadata and controls

94 lines (73 loc) · 2.66 KB
 
Nov 27, 2012
Nov 27, 2012
1
2
/*
Simple DirectMedia Layer
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
Nov 27, 2012
Nov 27, 2012
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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.
May 18, 2013
May 18, 2013
26
Used by the fuzzer component.
Nov 27, 2012
Nov 27, 2012
27
28
29
30
31
32
Original source code contributed by A. Schiffler for GSOC project.
*/
#include "SDL_config.h"
May 18, 2013
May 18, 2013
33
#include <stdlib.h>
Dec 1, 2012
Dec 1, 2012
34
#include <stdio.h>
Nov 27, 2012
Nov 27, 2012
35
36
37
38
39
40
41
42
43
#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;
May 18, 2013
May 18, 2013
44
Nov 27, 2012
Nov 27, 2012
45
46
47
48
49
/*
* Choose a value for 'a' from this list
* 1791398085 1929682203 1683268614 1965537969 1675393560
* 1967773755 1517746329 1447497129 1655692410 1606218150
* 2051013963 1075433238 1557985959 1781943330 1893513180
May 18, 2013
May 18, 2013
50
* 1631296680 2131995753 2083801278 1873196400 1554115554
Nov 27, 2012
Nov 27, 2012
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
*/
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;
May 18, 2013
May 18, 2013
68
Nov 27, 2012
Nov 27, 2012
69
if (rndContext==NULL) return;
May 18, 2013
May 18, 2013
70
Nov 27, 2012
Nov 27, 2012
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;
May 18, 2013
May 18, 2013
85
Nov 27, 2012
Nov 27, 2012
86
87
88
89
90
91
92
93
94
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);
}