Scopira 20080306

random.h

00001 
00002 /*
00003  *  Copyright (c) 2002    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INCLUDED__SCOPIRA_TOOL_RANDOM_H__
00015 #define __INCLUDED__SCOPIRA_TOOL_RANDOM_H__
00016 
00017 #include <stdlib.h>
00018 #include <string>
00019 
00020 #include <scopira/tool/object.h>
00021 #include <scopira/tool/limits.h>
00022 #include <scopira/tool/export.h>
00023 
00024 namespace scopira
00025 {
00026   namespace tool
00027   {
00035     SCOPIRA_EXPORT int time_seed(void);
00036 
00037     class sysdev_gen;
00038     class sysrand_gen;
00039     class sysrandom_gen;
00040 
00041     // alternates to distribution objects
00042 
00048     template <class GEN>
00049       double next_double(GEN &g, double mx) { return mx * (g() - g.min()) / (g.max() - g.min()); }
00055     template <class GEN>
00056       double next_double(GEN &g) { return (g() - g.min()) / (g.max() - g.min()); }
00057   }
00058 }
00059 
00066 class scopira::tool::sysdev_gen
00067 {
00068   public:
00069 
00070     typedef unsigned int result_type;
00071     
00072     static const bool has_fixed_range = true;
00073     //static const result_type min_value = std::numeric_limits<result_type>::min();
00074     //static const result_type max_value = std::numeric_limits<result_type>::max();
00075     
00081     SCOPIRA_EXPORT explicit sysdev_gen(const std::string& token = default_token);
00083     SCOPIRA_EXPORT ~sysdev_gen();
00084 
00086     result_type min(void) const { return std::numeric_limits<result_type>::min(); }
00088     result_type max(void) const { return std::numeric_limits<result_type>::max(); }
00089 
00094     SCOPIRA_EXPORT result_type operator()(void);
00101     result_type operator()(result_type mx) { return (*this)() % mx; }
00102 
00103   private:
00104     static const char * const default_token;
00105 
00106     class imp_t;
00107     imp_t * m_imp;
00108 };
00109 
00116 class scopira::tool::sysrand_gen
00117 {
00118   public:
00119     typedef int result_type;
00120     
00121     static const bool has_fixed_range = true;
00122     static const int min_value = 0;
00123     static const int max_value = RAND_MAX;
00124     
00126     SCOPIRA_EXPORT explicit sysrand_gen(unsigned int seed0 = 1);
00127 
00129     result_type min(void) const { return min_value; }
00131     result_type max(void) const { return max_value; }
00132 
00134     SCOPIRA_EXPORT static void seed(unsigned int seed0);
00135 
00136     SCOPIRA_EXPORT static result_type next(void);
00137 
00142     result_type operator()(void) { return next(); }
00149     result_type operator()(result_type mx) { return (*this)() % mx; }
00150 
00151     // not serializable
00152 };
00153 
00160 class scopira::tool::sysrandom_gen
00161 {
00162   public:
00163     typedef long int result_type;
00164 
00165     static const bool has_fixed_range = true;
00166     static const int min_value = 0;
00167     static const int max_value = RAND_MAX;
00168 
00170     SCOPIRA_EXPORT explicit sysrandom_gen(unsigned int seed0 = 1);
00171 
00173     result_type min(void) const { return min_value; }
00175     result_type max(void) const { return max_value; }
00176 
00178     SCOPIRA_EXPORT static void seed(unsigned int seed0);
00179 
00180     SCOPIRA_EXPORT result_type next(void);
00181 
00186     result_type operator()(void) { return next(); }
00193     result_type operator()(result_type mx) { return (*this)() % mx; }
00194 
00195     // not serializable
00196 };
00197 
00265 #endif
00266