Random.h
Go to the documentation of this file.
00001 /*      
00002  * File: Random.h
00003  * Project: DUtils library
00004  * Author: Dorian Galvez-Lopez
00005  * Date: April 2010
00006  * Description: manages pseudo-random numbers
00007  *
00008  * 
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU Lesser General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  */
00023 
00024 #pragma once
00025 #ifndef __D_RANDOM__
00026 #define __D_RANDOM__
00027 
00028 #include <cstdlib>
00029 
00030 namespace DUtils {
00031 
00032 class Random
00033 {
00034 public:
00038         static void SeedRand();
00039         
00044         static void SeedRandOnce();
00045 
00050         static void SeedRand(int seed);
00051 
00057         static void SeedRandOnce(int seed);
00058 
00063         template <class T>
00064         static T RandomValue(){
00065                 return (T)rand()/(T)RAND_MAX;
00066         }
00067 
00074         template <class T>
00075         static T RandomValue(T min, T max){
00076                 return Random::RandomValue<T>() * (max - min) + min;
00077         }
00078 
00085         static int RandomInt(int min, int max);
00086         
00092         template <class T>
00093         static T RandomGaussianValue(T mean, T sigma)
00094         {
00095           // Box-Muller transformation
00096     T x1, x2, w, y1;
00097     
00098     do {
00099       x1 = (T)2. * RandomValue<T>() - (T)1.;
00100       x2 = (T)2. * RandomValue<T>() - (T)1.;
00101       w = x1 * x1 + x2 * x2;
00102     } while ( w >= (T)1. || w == (T)0. );
00103 
00104     w = sqrt( ((T)-2.0 * log( w ) ) / w );
00105     y1 = x1 * w;
00106 
00107     return( mean + y1 * sigma );
00108         }
00109 
00110 private:
00111 
00113   static bool m_seeded_current_time;
00114   
00116   static bool m_seeded_int;
00117 
00118 };
00119 
00120 }
00121 
00122 #endif
00123 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:17