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 "scripting/random_generator.hpp"
38 #include "script_interface.hpp"
40 class RandomGenerator : public Scripting::RandomGenerator
43 // Array versions of the above information to make code run faster --
44 // relies on fact that TYPE_i == i.
45 static const int TYPE_0 = 0; // Linear congruential
46 static const int BREAK_0 = 8;
47 static const int DEG_0 = 0;
48 static const int SEP_0 = 0;
50 static const int TYPE_1 = 1; // x**7 + x**3 + 1
51 static const int BREAK_1 = 32;
52 static const int DEG_1 = 7;
53 static const int SEP_1 = 3;
55 static const int TYPE_2 = 2; // x**15 + x + 1
56 static const int BREAK_2 = 64;
57 static const int DEG_2 = 15;
58 static const int SEP_2 = 1;
60 static const int TYPE_3 = 3; // x**31 + x**3 + 1
61 static const int BREAK_3 = 128;
62 static const int DEG_3 = 31;
63 static const int SEP_3 = 3;
65 static const int TYPE_4 = 4; // x**63 + x + 1
66 static const int BREAK_4 = 256;
67 static const int DEG_4 = 63;
68 static const int SEP_4 = 1;
70 static const int MAX_TYPES = 5; // Max number of types above
73 long degrees[MAX_TYPES];
74 long seps [MAX_TYPES];
75 long randtbl[DEG_3 + 1];
90 // Documentation of user-visible calls:
92 // Initialize the RNG with a 31-bit seed
93 // if x is zero or absent, calls to time() will get a time-randomized seed
94 // the value returned is the value of the seed used.
97 // generate random 31-bit numbers
98 // calls to the following return a value evenly distributed between u (or
99 // 0 if not specified) and v (or RAND_MAX if not specified). Return
100 // values may include u, but never v.
103 int rand(int u, int v);
104 double randf(double v);
105 double randf(double u, double v);
107 // For Squirrel wrapper, since miniswig (and even squirrel?) doesn't
108 // support function overloading or doubles
109 int rand1i(int v) { return rand(v); }
110 int rand2i(int u, int v) { return rand(u, v); }
111 float rand1f(float v)
112 { return static_cast<float>(randf(static_cast<double>(v))); }
113 float rand2f(float u, float v)
114 { return static_cast<float>(randf(static_cast<double>(u),
115 static_cast<double>(v))); }
119 void srandom(unsigned long x);
120 // void srandomdev();
121 // char *initstate(unsigned long seed, char *arg_state, long n);
122 // char *setstate(char *arg_state);
126 extern RandomGenerator systemRandom;
128 #endif //__RANDOM_GENERATOR__