summaryrefslogtreecommitdiff
path: root/libc/stdlib/rand.c
blob: b5c5cb76425a123263065de129de046794995734 (plain)
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
#ifdef ZX81_RNG
/*
 * This is my favorite tiny RNG, If you had a ZX81 you may recognise it :-)
 *								(RdeBath)
 */

#include <stdlib.h>

#define MAXINT (((unsigned)-1)>>1)

static unsigned int sseed = 0;

int rand()
{
	return (sseed = (((sseed + 1L) * 75L) % 65537L) - 1) & MAXINT;
}

void srand(seed)
unsigned int seed;
{
	sseed = seed;
}

#else

/*
 * This generator is a combination of three linear congruential generators
 * with periods or 2^15-405, 2^15-1041 and 2^15-1111. It has a period that
 * is the product of these three numbers.
 */

static int seed1 = 1;
static int seed2 = 1;
static int seed3 = 1;

#define MAXINT (((unsigned)-1)>>1)

#define CRANK(a,b,c,m,s) 	\
	q = s/a;		\
	s = b*(s-a*q) - c*q;	\
	if(s<0) s+=m;

int rand()
{
	register int q;

	CRANK(206, 157, 31, 32363, seed1);
	CRANK(217, 146, 45, 31727, seed2);
	CRANK(222, 142, 133, 31657, seed3);

	return seed1 ^ seed2 ^ seed3;
}

void srand(seed)
unsigned int seed;
{
	seed &= MAXINT;
	seed1 = seed % 32362 + 1;
	seed2 = seed % 31726 + 1;
	seed3 = seed % 31656 + 1;
}

#endif