Merge branch 'master' of github.com:thepowersgang/acess2
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / rand.c
1 /*
2  * Acess2 Lib C
3  * - By John Hodge (thePowersGang)
4  *
5  * rand.c
6  * - srand/rand/rand_p functions
7  */
8 #include <stdlib.h>
9
10 // === GLOBALS ===
11 unsigned int    _rand_state_x = 123456789;
12 unsigned int    _rand_state_y = 362436069;
13 unsigned int    _rand_state_z = 521288629;
14 unsigned int    _rand_state_w = 88675123;
15
16 // === FUNCTIONS ===
17 int rand_p(unsigned int *seedp)
18 {
19         const int const_a = 0x731ADE, const_c = 12345;
20         // Linear Congruency
21         *seedp = *seedp * const_a + const_c;
22         return *seedp;
23 }
24
25 void srand(unsigned int seed)
26 {
27         _rand_state_x = rand_p( &seed );
28         _rand_state_y = rand_p( &seed );
29         _rand_state_z = rand_p( &seed );
30         _rand_state_w = rand_p( &seed );
31 }
32
33 int rand(void)
34 {
35         unsigned int t;
36         
37         t = _rand_state_x ^ (_rand_state_x << 11);
38         _rand_state_x = _rand_state_y; _rand_state_y = _rand_state_z; _rand_state_z = _rand_state_w;
39         return _rand_state_w = _rand_state_w ^ (_rand_state_w >> 19) ^ t ^ (t >> 8); 
40 }
41

UCC git Repository :: git.ucc.asn.au