FRED
|
00001 /* 00002 Copyright 2009 by the University of Pittsburgh 00003 Licensed under the Academic Free License version 3.0 00004 See the file "LICENSE" for more information 00005 */ 00006 00007 // 00008 // 00009 // File: Random.h 00010 // 00011 00012 #ifndef _FRED_RANDOM_H 00013 #define _FRED_RANDOM_H 00014 00015 #include <math.h> 00016 #include <vector> 00017 00018 using namespace std; 00019 00020 //*************** RANDOM NUMBER GENERATOR UTILITIES 00021 00022 /* Using Marseinne Twister MT19937 by T. Nishimura and M. Matsumoto */ 00023 /* See mt19937ar.c for acknowledgements */ 00024 00025 double genrand_real2(); 00026 void init_genrand(unsigned long s); 00027 #define INIT_RANDOM(SEED) init_genrand(SEED) 00028 #define RANDOM() genrand_real2() 00029 #define IRAND(LOW,HIGH) ((int)((LOW)+(int)(((HIGH)-(LOW)+1)*RANDOM()))) 00030 #define URAND(LOW,HIGH) ((double)((LOW)+(((HIGH)-(LOW))*RANDOM()))) 00031 00032 int draw_poisson(double lambda); 00033 double draw_exponential(double lambda); 00034 int draw_from_distribution(int n, double *dist); 00035 double draw_standard_normal(); 00036 double draw_normal(double mu, double sigma); 00037 int draw_from_cdf(double *v, int size); 00038 int draw_from_cdf_vector(const vector <double>& v); 00039 00040 template <typename T> 00041 void FYShuffle( vector <T> &array){ 00042 int m,randIndx; 00043 T tmp; 00044 unsigned int n = array.size(); 00045 m=n; 00046 while (m > 0){ 00047 randIndx = (int)(RANDOM()*n); 00048 m--; 00049 tmp = array[m]; 00050 array[m] = array[randIndx]; 00051 array[randIndx] = tmp; 00052 } 00053 } 00054 00055 #endif // _FRED_RANDOM_H