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