00001
00008 #ifndef __VGEN_H__
00009 #define __VGEN_H__
00010
00011 #include <cmath>
00012 #include <vector>
00013 #include <algorithm>
00014 #include <functional>
00015
00016 #include "genericalgs.h"
00017
00018 #ifdef ANSI_COMPATIBLE
00019
00020 #include "ind_types.h"
00021 #endif
00022
00023
00032 class ValueGen {
00033 public:
00034 ValueGen() { };
00035 virtual ~ValueGen() { }
00037 virtual bool next()=0;
00039 virtual void reset()=0;
00040 };
00041
00042
00043
00045 class ValueGenContainer : public ValueGen {
00046 public:
00047 virtual ~ValueGenContainer() { Generic::delete_contents(c); }
00048
00049 virtual void reset()
00050 { std::for_each(c.begin(),c.end(),std::mem_fun(&ValueGen::reset)); }
00051
00052 virtual bool next()
00053 { return std::count_if(c.begin(),c.end(),std::mem_fun(&ValueGen::next)); }
00054
00057 virtual void push_back(ValueGen* v) { c.push_back(v); }
00058
00059 protected:
00061 std::vector< ValueGen* > c;
00062 };
00063
00064
00065
00079 template<class T>
00080 T shuffled_rand( bool initialize, T=0.0)
00081 {
00082 static T y, v[198];
00083 static bool is_initialized = false;
00084 int i;
00085
00086 if (initialize || is_initialized == false) {
00087 for (i=1; i<=197; i++) v[i]= drand48();
00088 y = drand48();
00089 is_initialized = true;
00090 }
00091 i = (int)floor(1.0 + 197.0 * y);
00092 assert(!(i > 197 || i < 1));
00093
00094 y = v[i];
00095
00096 v[i] = drand48();
00097 return(y);
00098 }
00099
00100
00101
00103 inline double shuffled_rand( bool initialize=false)
00104 { return shuffled_rand<double>(initialize); }
00105
00106
00107
00109 inline void shuffled_rand_reset(long int new_seed)
00110 {
00111 srand48(new_seed);
00112 shuffled_rand(true);
00113 }
00114
00115
00116 #endif