Scopira  20080306
random.h
1 
2 /*
3  * Copyright (c) 2002 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED__SCOPIRA_TOOL_RANDOM_H__
15 #define __INCLUDED__SCOPIRA_TOOL_RANDOM_H__
16 
17 #include <stdlib.h>
18 #include <string>
19 
20 #include <scopira/tool/object.h>
21 #include <scopira/tool/limits.h>
22 #include <scopira/tool/export.h>
23 
24 namespace scopira
25 {
26  namespace tool
27  {
35  SCOPIRA_EXPORT int time_seed(void);
36 
37  class sysdev_gen;
38  class sysrand_gen;
39  class sysrandom_gen;
40 
41  // alternates to distribution objects
42 
48  template <class GEN>
49  double next_double(GEN &g, double mx) { return mx * (g() - g.min()) / (g.max() - g.min()); }
55  template <class GEN>
56  double next_double(GEN &g) { return (g() - g.min()) / (g.max() - g.min()); }
57  }
58 }
59 
67 {
68  public:
69 
70  typedef unsigned int result_type;
71 
72  static const bool has_fixed_range = true;
73  //static const result_type min_value = std::numeric_limits<result_type>::min();
74  //static const result_type max_value = std::numeric_limits<result_type>::max();
75 
81  SCOPIRA_EXPORT explicit sysdev_gen(const std::string& token = default_token);
83  SCOPIRA_EXPORT ~sysdev_gen();
84 
86  result_type min(void) const { return std::numeric_limits<result_type>::min(); }
88  result_type max(void) const { return std::numeric_limits<result_type>::max(); }
89 
94  SCOPIRA_EXPORT result_type operator()(void);
101  result_type operator()(result_type mx) { return (*this)() % mx; }
102 
103  private:
104  static const char * const default_token;
105 
106  class imp_t;
107  imp_t * m_imp;
108 };
109 
117 {
118  public:
119  typedef int result_type;
120 
121  static const bool has_fixed_range = true;
122  static const int min_value = 0;
123  static const int max_value = RAND_MAX;
124 
126  SCOPIRA_EXPORT explicit sysrand_gen(unsigned int seed0 = 1);
127 
129  result_type min(void) const { return min_value; }
131  result_type max(void) const { return max_value; }
132 
134  SCOPIRA_EXPORT static void seed(unsigned int seed0);
135 
136  SCOPIRA_EXPORT static result_type next(void);
137 
142  result_type operator()(void) { return next(); }
149  result_type operator()(result_type mx) { return (*this)() % mx; }
150 
151  // not serializable
152 };
153 
161 {
162  public:
163  typedef long int result_type;
164 
165  static const bool has_fixed_range = true;
166  static const int min_value = 0;
167  static const int max_value = RAND_MAX;
168 
170  SCOPIRA_EXPORT explicit sysrandom_gen(unsigned int seed0 = 1);
171 
173  result_type min(void) const { return min_value; }
175  result_type max(void) const { return max_value; }
176 
178  SCOPIRA_EXPORT static void seed(unsigned int seed0);
179 
180  SCOPIRA_EXPORT result_type next(void);
181 
186  result_type operator()(void) { return next(); }
193  result_type operator()(result_type mx) { return (*this)() % mx; }
194 
195  // not serializable
196 };
197 
265 #endif
266 
sysdev_gen(const std::string &token=default_token)
Definition: archiveflow.h:20
Definition: random.h:66
double next_double(GEN &g, double mx)
Definition: random.h:49
result_type min(void) const
min val
Definition: random.h:129
result_type operator()(void)
Definition: random.h:116
int time_seed(void)
result_type max(void) const
max val
Definition: random.h:175
result_type operator()(result_type mx)
Definition: random.h:149
result_type operator()(void)
Definition: random.h:186
result_type min(void) const
min val
Definition: random.h:86
result_type max(void) const
max val
Definition: random.h:88
result_type operator()(result_type mx)
Definition: random.h:101
Definition: random.h:160
result_type max(void) const
max val
Definition: random.h:131
result_type operator()(result_type mx)
Definition: random.h:193
result_type operator()(void)
Definition: random.h:142
result_type min(void) const
min val
Definition: random.h:173