OSDN Git Service

Initial revision
[uclinux-h8/uClibc.git] / libc / stdlib / rand.c
1 #ifdef ZX81_RNG
2 /*
3  * This is my favorite tiny RNG, If you had a ZX81 you may recognise it :-)
4  *                                                              (RdeBath)
5  */
6
7 #include <stdlib.h>
8
9 #define MAXINT (((unsigned)-1)>>1)
10
11 static unsigned int sseed = 0;
12
13 int rand()
14 {
15    return ( sseed = (((sseed+1L)*75L)%65537L)-1 ) & MAXINT;
16 }
17
18 void srand(seed)
19 unsigned int seed;
20 {
21    sseed=seed;
22 }
23
24 #else
25
26 /*
27  * This generator is a combination of three linear congruential generators
28  * with periods or 2^15-405, 2^15-1041 and 2^15-1111. It has a period that
29  * is the product of these three numbers.
30  */
31
32 static int seed1 = 1;
33 static int seed2 = 1;
34 static int seed3 = 1;
35 #define MAXINT (((unsigned)-1)>>1)
36
37 #define CRANK(a,b,c,m,s)        \
38         q = s/a;                \
39         s = b*(s-a*q) - c*q;    \
40         if(s<0) s+=m;
41
42 int rand()
43 {
44    register int q, z;
45    CRANK(206, 157,  31, 32363, seed1);
46    CRANK(217, 146,  45, 31727, seed2);
47    CRANK(222, 142, 133, 31657, seed3);
48
49    return seed1^seed2^seed3;
50 }
51
52 void srand(seed)
53 unsigned int seed;
54 {
55    seed &= MAXINT;
56    seed1= seed%32362 + 1;
57    seed2= seed%31726 + 1;
58    seed3= seed%31656 + 1;
59 }
60
61 #endif