OSDN Git Service

add sdl-1.3
[android-x86/external-stagefright-plugins.git] / SDL-1.3 / android-project / jni / SDL / test / test-automation / src / libSDLtest / fuzzer / utl_random.c
1
2 /* 
3
4  utl_random
5
6  A "32-bit Multiply with carry" random number generator. 
7  
8 */
9
10
11 #include "utl_random.h"
12
13
14 /* Initialize random number generator with two integer variables */
15
16 void utl_randomInit(RND_CTX * rndContext, unsigned int xi, unsigned int ci)
17 {
18   /*
19    * Choose a value for 'a' from this list
20    * 1791398085 1929682203 1683268614 1965537969 1675393560
21    * 1967773755 1517746329 1447497129 1655692410 1606218150
22    * 2051013963 1075433238 1557985959 1781943330 1893513180
23    * 1631296680 2131995753 2083801278 1873196400 1554115554 
24    */
25   rndContext->a = 1655692410;
26   rndContext->x = 30903;
27   rndContext->c = 0;
28   if (xi != 0)
29     rndContext->x = xi;
30   rndContext->c = ci;
31   rndContext->ah = rndContext->a >> 16;
32   rndContext->al = rndContext->a & 65535;
33 }
34
35 /* Initialize random number generator from time */
36
37 void utl_randomInitTime(RND_CTX * rndContext)
38 {
39   int a,b;
40   
41   srand(time(NULL));
42   a=rand();
43   srand(clock());
44   b=rand();
45   utl_randomInit(rndContext, a, b);
46 }
47
48 /* Returns random numbers */
49
50 unsigned int utl_random(RND_CTX * rndContext)
51 {
52   unsigned int xh, xl;
53
54   xh = rndContext->x >> 16, xl = rndContext->x & 65535;
55   rndContext->x = rndContext->x * rndContext->a + rndContext->c;
56   rndContext->c =
57     xh * rndContext->ah + ((xh * rndContext->al) >> 16) +
58     ((xl * rndContext->ah) >> 16);
59   if (xl * rndContext->al >= (~rndContext->c + 1))
60     rndContext->c++;
61
62   return (rndContext->x);
63 }