3 // A strong random number generator
5 // Copyright (C) 2006 Allen King
6 // Copyright (C) 2002 Michael Ringgaard. All rights reserved.
7 // Copyright (C) 1983, 1993 The Regents of the University of California.
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 // 2. Redistributions in binary form must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the distribution.
18 // 3. Neither the name of the project nor the names of its contributors
19 // may be used to endorse or promote products derived from this software
20 // without specific prior written permission.
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
26 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #ifndef __RANDOM_GENERATOR__
35 #define __RANDOM_GENERATOR__
37 #include "script_interface.hpp"
42 // Array versions of the above information to make code run faster --
43 // relies on fact that TYPE_i == i.
44 static const int TYPE_0 = 0; // Linear congruential
45 static const int BREAK_0 = 8;
46 static const int DEG_0 = 0;
47 static const int SEP_0 = 0;
49 static const int TYPE_1 = 1; // x**7 + x**3 + 1
50 static const int BREAK_1 = 32;
51 static const int DEG_1 = 7;
52 static const int SEP_1 = 3;
54 static const int TYPE_2 = 2; // x**15 + x + 1
55 static const int BREAK_2 = 64;
56 static const int DEG_2 = 15;
57 static const int SEP_2 = 1;
59 static const int TYPE_3 = 3; // x**31 + x**3 + 1
60 static const int BREAK_3 = 128;
61 static const int DEG_3 = 31;
62 static const int SEP_3 = 3;
64 static const int TYPE_4 = 4; // x**63 + x + 1
65 static const int BREAK_4 = 256;
66 static const int DEG_4 = 63;
67 static const int SEP_4 = 1;
69 static const int MAX_TYPES = 5; // Max number of types above
72 long degrees[MAX_TYPES];
73 long seps [MAX_TYPES];
74 long randtbl[DEG_3 + 1];
85 static const int rand_max = 0x7fffffff; // biggest signed Uint32
91 // Documentation of user-visible calls:
93 // Initialize the RNG with a 31-bit seed
94 // if x is zero or absent, calls to time() will get a time-randomized seed
95 // the value returned is the value of the seed used.
98 // generate random 31-bit numbers
99 // calls to the following return a value evenly distributed between u (or
100 // 0 if not specified) and v (or rand_max if not specified). Return
101 // values may include u, but never v.
104 int rand(int u, int v);
105 double randf(double v);
106 double randf(double u, double v);
108 // For Squirrel wrapper, since miniswig (and even squirrel?) doesn't
109 // support function overloading or doubles
110 int rand1i(int v) { return rand(v); }
111 int rand2i(int u, int v) { return rand(u, v); }
112 float rand1f(float v)
113 { return static_cast<float>(randf(static_cast<double>(v))); }
114 float rand2f(float u, float v)
115 { return static_cast<float>(randf(static_cast<double>(u),
116 static_cast<double>(v))); }
120 void srandom(unsigned long x);
121 // void srandomdev();
122 // char *initstate(unsigned long seed, char *arg_state, long n);
123 // char *setstate(char *arg_state);
127 extern RandomGenerator systemRandom;
129 #endif //__RANDOM_GENERATOR__