00001 // $Id: random_generator.hpp 6382 2010-02-21 23:18:32Z mathnerd314 $ 00002 // 00003 // A strong random number generator 00004 // 00005 // Copyright (C) 2006 Allen King 00006 // Copyright (C) 2002 Michael Ringgaard. All rights reserved. 00007 // Copyright (C) 1983, 1993 The Regents of the University of California. 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions 00011 // are met: 00012 // 00013 // 1. Redistributions of source code must retain the above copyright 00014 // notice, this list of conditions and the following disclaimer. 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 3. Neither the name of the project nor the names of its contributors 00019 // may be used to endorse or promote products derived from this software 00020 // without specific prior written permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00023 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00024 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00025 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00026 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00027 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00028 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00029 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00031 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00032 // SUCH DAMAGE. 00033 00034 #ifndef HEADER_SUPERTUX_MATH_RANDOM_GENERATOR_HPP 00035 #define HEADER_SUPERTUX_MATH_RANDOM_GENERATOR_HPP 00036 00037 class RandomGenerator 00038 { 00039 private: 00040 // Array versions of the above information to make code run faster -- 00041 // relies on fact that TYPE_i == i. 00042 static const int TYPE_0 = 0; // Linear congruential 00043 static const int BREAK_0 = 8; 00044 static const int DEG_0 = 0; 00045 static const int SEP_0 = 0; 00046 00047 static const int TYPE_1 = 1; // x**7 + x**3 + 1 00048 static const int BREAK_1 = 32; 00049 static const int DEG_1 = 7; 00050 static const int SEP_1 = 3; 00051 00052 static const int TYPE_2 = 2; // x**15 + x + 1 00053 static const int BREAK_2 = 64; 00054 static const int DEG_2 = 15; 00055 static const int SEP_2 = 1; 00056 00057 static const int TYPE_3 = 3; // x**31 + x**3 + 1 00058 static const int BREAK_3 = 128; 00059 static const int DEG_3 = 31; 00060 static const int SEP_3 = 3; 00061 00062 static const int TYPE_4 = 4; // x**63 + x + 1 00063 static const int BREAK_4 = 256; 00064 static const int DEG_4 = 63; 00065 static const int SEP_4 = 1; 00066 00067 static const int MAX_TYPES = 5; // Max number of types above 00068 00069 bool initialized; 00070 long degrees[MAX_TYPES]; 00071 long seps [MAX_TYPES]; 00072 long randtbl[DEG_3 + 1]; 00073 00074 long *fptr; 00075 long *rptr; 00076 00077 long *state; 00078 long rand_type; 00079 long rand_deg; 00080 long rand_sep; 00081 long *end_ptr; 00082 int debug; 00083 static const int rand_max = 0x7fffffff; // biggest signed Uint32 00084 00085 public: 00086 RandomGenerator(); 00087 ~RandomGenerator(); 00088 00089 // Documentation of user-visible calls: 00090 00091 // Initialize the RNG with a 31-bit seed 00092 // if x is zero or absent, calls to time() will get a time-randomized seed 00093 // the value returned is the value of the seed used. 00094 int srand(int x=0); 00095 00096 // generate random 31-bit numbers 00097 // calls to the following return a value evenly distributed between u (or 00098 // 0 if not specified) and v (or rand_max if not specified). Return 00099 // values may include u, but never v. 00100 int rand(); 00101 int rand(int v); 00102 int rand(int u, int v); 00103 double randf(double v); 00104 double randf(double u, double v); 00105 00106 // For Squirrel wrapper, since miniswig (and even squirrel?) doesn't 00107 // support function overloading or doubles 00108 int rand1i(int v) { return rand(v); } 00109 int rand2i(int u, int v) { return rand(u, v); } 00110 float rand1f(float v) 00111 { return static_cast<float>(randf(static_cast<double>(v))); } 00112 float rand2f(float u, float v) 00113 { return static_cast<float>(randf(static_cast<double>(u), 00114 static_cast<double>(v))); } 00115 00116 //private: 00117 void initialize(); 00118 void srandom(unsigned long x); 00119 // void srandomdev(); 00120 // char *initstate(unsigned long seed, char *arg_state, long n); 00121 // char *setstate(char *arg_state); 00122 long random(); 00123 00124 private: 00125 RandomGenerator(const RandomGenerator&); 00126 RandomGenerator& operator=(const RandomGenerator&); 00127 }; 00128 00129 // Use for random particle fx or whatever 00130 extern RandomGenerator graphicsRandom; 00131 // Use for game-changing random numbers 00132 extern RandomGenerator gameRandom; 00133 00134 #endif //__RANDOM_GENERATOR__ 00135 00136 /* EOF */