00001 /********************************************************************* 00002 * lescegra * 00003 * * 00004 * http://geeky.kicks-ass.org/projects/lescegra.html * 00005 * * 00006 * Copyright 2003 by Enno Cramer <uebergeek@web.de> * 00007 * * 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Library General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Library General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Library General Public * 00019 * License along with this library; if not, write to the Free * 00020 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * 00021 *********************************************************************/ 00022 00023 #ifndef LSG_RANDOM_H 00024 #define LSG_RANDOM_H 1 00025 00026 /** 00027 * \file random.h 00028 * \brief Pseudo random number generator 00029 */ 00030 00031 #include <lescegra/util/object.h> 00032 00033 #define LSG_RANDOM_STATE_SIZE 624 00034 00035 /** 00036 * \ingroup util 00037 * \brief Pseudo random number generator 00038 * 00039 * A pseudo random number generator utilizing the Mersenne Twister algorithm. 00040 * \see http://www.math.keio.ac.jp/~matumoto/emt.html 00041 */ 00042 typedef struct { 00043 LsgObject super; 00044 unsigned long int state[LSG_RANDOM_STATE_SIZE]; 00045 int i; 00046 } LsgRandom; 00047 00048 /** 00049 * \relates LsgRandom 00050 * Allocate and initialize a new LsgRandom instance with the given seed value. 00051 * @param seed The seed value 00052 * @return a new LsgRandom instance 00053 */ 00054 LsgRandom* LsgRandom_create(unsigned long int seed); 00055 00056 /** 00057 * \relates LsgRandom 00058 * Initialize the instance with a given seed. 00059 * @param self The instance variable 00060 * @param seed The seed value 00061 */ 00062 void LsgRandom_init(LsgRandom* self, unsigned long int seed); 00063 00064 /** 00065 * \relates LsgRandom 00066 * Return a random \c unsigned \c long \c int between \c 0 and \c MAX_ULONG 00067 * inclusive. 00068 * @param self The instance variable 00069 * @return a random value between \c 0 and \c MAX_ULONG 00070 */ 00071 unsigned long int LsgRandom_generate(LsgRandom* self); 00072 00073 /** 00074 * \relates LsgRandom 00075 * Return a random \c float between \c 0.0 and \c 1.0 inclusive. 00076 * @param self The instance variable 00077 * @return a random value between \c 0.0 and \c 1.0 00078 */ 00079 float LsgRandom_random(LsgRandom* self); 00080 00081 /** 00082 * \relates LsgRandom 00083 * Return a random \c float between \c 0.0 and a given maximal value (inclusive). 00084 * @param self The instance variable 00085 * @param self The highest allowed return value 00086 * @return a random value between \c 0.0 and \a limit 00087 */ 00088 float LsgRandom_randomMax(LsgRandom* self, float limit); 00089 00090 /** 00091 * \relates LsgRandom 00092 * Return a random \c float between \c -1.0 and \c +1.0 inclusive. 00093 * @param self The instance variable 00094 * @return a random value between \c -1.0 and \c +1.0 00095 */ 00096 float LsgRandom_randomError(LsgRandom* self); 00097 00098 /** 00099 * \relates LsgRandom 00100 * Return a random \c float from a given interval. 00101 * @param self The instance variable 00102 * @param base The center of the interval 00103 * @param error The maximum distance between the return value and \a base 00104 * @return a random value from the interval [ \a base - \a error, \a base + \a error ] 00105 */ 00106 float LsgRandom_randomRange(LsgRandom* self, float base, float error); 00107 00108 /** 00109 * Seed the random number generator. 00110 * @param seed The new seed 00111 */ 00112 void random_seed(unsigned long int seed); 00113 00114 /** 00115 * Compute a pseudo random float between 0.0 and 1.0 exclusive. 00116 * @return A random float in the range [0.0, 1.0] 00117 */ 00118 float random(void); 00119 00120 /** 00121 * Compute a pseudo random number between 0.0 and a given maximum. 00122 * @param max The upper limit for the generated random number 00123 * @return A random number in the range [0.0, max] 00124 */ 00125 float random_max(float max); 00126 00127 /** 00128 * Compute a pseudo random number between -1.0 and +1.0. 00129 * @param A random number in the range [-1.0, +1.0] 00130 */ 00131 float random_error(void); 00132 00133 /** 00134 * Compute a pseudo random number in a range defined by its center and extent. 00135 * @param base The center of the range 00136 * @param error The extent of the range in each direction 00137 * @return A random number in the range [base - error, base + error] 00138 */ 00139 float random_range(float base, float error); 00140 00141 #endif