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 class using the Mersenne Twister 00038 * algorithm. 00039 */ 00040 typedef struct { 00041 LsgObject super; 00042 unsigned long int state[LSG_RANDOM_STATE_SIZE]; 00043 int i; 00044 } LsgRandom; 00045 00046 LsgRandom* LsgRandom_create(unsigned long int seed); 00047 void LsgRandom_init(LsgRandom* self, unsigned long int seed); 00048 unsigned long int LsgRandom_generate(LsgRandom* self); 00049 float LsgRandom_random(LsgRandom* self); 00050 float LsgRandom_randomMax(LsgRandom* self, float limit); 00051 float LsgRandom_randomError(LsgRandom* self); 00052 float LsgRandom_randomRange(LsgRandom* self, float base, float error); 00053 00054 /** 00055 * Seed the random number generator. 00056 * @param seed The new seed 00057 */ 00058 void random_seed(unsigned long int seed); 00059 00060 /** 00061 * Compute a pseudo random float between 0.0 and 1.0 exclusive. 00062 * @return A random float in the range [0.0, 1.0] 00063 */ 00064 float random(void); 00065 00066 /** 00067 * Compute a pseudo random number between 0.0 and a given maximum. 00068 * @param max The upper limit for the generated random number 00069 * @return A random number in the range [0.0, max] 00070 */ 00071 float random_max(float max); 00072 00073 /** 00074 * Compute a pseudo random number between -1.0 and +1.0. 00075 * @param A random number in the range [-1.0, +1.0] 00076 */ 00077 float random_error(void); 00078 00079 /** 00080 * Compute a pseudo random number in a range defined by its center and extent. 00081 * @param base The center of the range 00082 * @param error The extent of the range in each direction 00083 * @return A random number in the range [base - error, base + error] 00084 */ 00085 float random_range(float base, float error); 00086 00087 #endif