1 /*
2 * SDLRoids - An Astroids clone.
3 *
4 * Copyright (c) 2000 David Hedbor <david@hedbor.org>
5 * based on xhyperoid by Russel Marks.
6 * xhyperoid is based on a Win16 game, Hyperoid by Edward Hutchins
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 /*
20 * rand.c - Custom random number generator, from Pike (http://pike.roxen.com/).
21 */
22
23 #include "config.h"
24 RCSID("$Id: rand.c,v 1.2 2000/07/17 02:22:16 neotron Exp $");
25
26 static unsigned long RandSeed1 = 0x5c2582a4;
27 static unsigned long RandSeed2 = 0x64dff8ca;
28
slow_rand(void)29 static unsigned long slow_rand(void)
30 {
31 RandSeed1 = ((RandSeed1 * 13 + 1) ^ (RandSeed1 >> 9)) + RandSeed2;
32 RandSeed2 = (RandSeed2 * RandSeed1 + 13) ^ (RandSeed2 >> 13);
33 return RandSeed1;
34 }
35
slow_srand(long seed)36 static void slow_srand(long seed)
37 {
38 RandSeed1 = (seed - 1) ^ 0xA5B96384UL;
39 RandSeed2 = (seed + 1) ^ 0x56F04021UL;
40 }
41
42 #define RNDBUF 250
43 #define RNDSTEP 7
44 #define RNDJUMP 103
45
46 static unsigned long rndbuf[ RNDBUF ];
47 static int rnd_index;
48
my_srand(long seed)49 void my_srand(long seed)
50 {
51 int e;
52 unsigned long mask;
53
54 slow_srand(seed);
55
56 rnd_index = 0;
57 for (e=0;e < RNDBUF; e++) rndbuf[e]=slow_rand();
58
59 mask = (unsigned long) -1;
60
61 for (e=0;e< (int)sizeof(long)*8 ;e++)
62 {
63 int d = RNDSTEP * e + 3;
64 rndbuf[d % RNDBUF] &= mask;
65 mask>>=1;
66 rndbuf[d % RNDBUF] |= (mask+1);
67 }
68 }
69
my_rand(unsigned long range)70 unsigned long my_rand(unsigned long range)
71 {
72 if( ++rnd_index == RNDBUF) rnd_index=0;
73 return (rndbuf[rnd_index] += rndbuf[rnd_index+RNDJUMP-(rnd_index<RNDBUF-RNDJUMP?0:RNDBUF)]) % range;
74 }
75
76