gKitGL
|
00001 00002 #ifndef _SAMPLER_H 00003 #define _SAMPLER_H 00004 00005 #include <cstdlib> 00006 #include <time.h> 00007 00008 00009 namespace gk { 00010 00011 #ifdef WIN32 00012 //! representation d'un generateur aleatoire ... simpliste, voire inutile 00013 class Sampler 00014 { 00015 public: 00016 Sampler( ) {} 00017 00018 //! genere un reel entre 0 et 1. 00019 float uniformFloat( ) 00020 { 00021 return (float) rand() / (float) RAND_MAX; 00022 } 00023 00024 //! genere un entier entre 0 et max. 00025 int uniformInt( const int max ) 00026 { 00027 return ((float) rand() * (float) max / (float) RAND_MAX) + .5f; 00028 } 00029 00030 //! initialisation du germe du generateur aleatoire. 00031 static void init( const int seed= 0 ) 00032 { 00033 if(seed != 0) 00034 srand(seed); 00035 else 00036 srand(time(NULL)); 00037 } 00038 }; 00039 00040 #else 00041 //! representation d'un generateur aleatoire ... simpliste, voire inutile 00042 class Sampler 00043 { 00044 public: 00045 Sampler( ) {} 00046 00047 //! genere un reel entre 0 et 1. 00048 float uniformFloat( ) 00049 { 00050 return drand48(); 00051 } 00052 00053 //! genere un entier entre 0 et max. 00054 int uniformInt( const int max ) 00055 { 00056 return (drand48() * (float) max + .5f); 00057 } 00058 00059 //! initialisation du germe du generateur aleatoire. 00060 static void init( const int seed= 0 ) 00061 { 00062 if(seed != 0) 00063 srand48(seed); 00064 else 00065 srand48(time(NULL)); 00066 } 00067 }; 00068 00069 #endif 00070 } 00071 00072 #endif