-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathrandom.c
64 lines (50 loc) · 970 Bytes
/
random.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Elite - The New Kind.
*
* Reverse engineered from the BBC disk version of Elite.
* Additional material by C.J.Pinder.
*
* The original Elite code is (C) I.Bell & D.Braben 1984.
* This version re-engineered in C by C.J.Pinder 1999-2001.
*
* email: <[email protected]>
*
*
*/
/*
* random.c
*/
#include <stdlib.h>
#include "allegro.h"
#include "random.h"
static int rand_seed;
/*
* Portable random number generator implementing the recursion:
* IX = 16807 * IX MOD (2**(31) - 1)
* Using only 32 bits, including sign.
*
* Taken from "A Guide to Simulation" by Bratley, Fox and Schrage.
*/
int randint (void)
{
int k1;
int ix = rand_seed;
k1 = ix / 127773;
ix = 16807 * (ix - k1 * 127773) - k1 * 2836;
if (ix < 0)
ix += 2147483647;
rand_seed = ix;
return ix;
}
void set_rand_seed (int seed)
{
rand_seed = seed;
}
int get_rand_seed (void)
{
return rand_seed;
}
int rand255 (void)
{
return (randint() & 255);
}