Stxxl  1.3.2
rand.h
1 /***************************************************************************
2  * include/stxxl/bits/common/rand.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002, 2003, 2005 Roman Dementiev <[email protected]>
7  * Copyright (C) 2007 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_RAND_HEADER
15 #define STXXL_RAND_HEADER
16 
17 #include <cstdlib>
18 
19 #ifdef STXXL_BOOST_RANDOM
20  #include <boost/random.hpp>
21 #endif
22 
23 #include <stxxl/bits/common/types.h>
24 #include <stxxl/bits/common/seed.h>
25 
26 // Recommended seeding procedure:
27 // by default, the global seed is initialized from a high resolution timer and the process id
28 // 1. stxxl::set_seed(seed); // optionally, do this if you wan't to us a specific seed to replay a certain program run
29 // 2. seed = stxxl::get_next_seed(); // store/print/... this value can be used for step 1 to replay the program with a specific seed
30 // 3. stxxl::srandom_number32(); // seed the global state of stxxl::random_number32
31 // 4. create all the other prngs used.
32 
33 
34 __STXXL_BEGIN_NAMESPACE
35 
36 extern unsigned ran32State;
37 
42 {
43  typedef unsigned value_type;
44 
46  inline value_type operator () () const
47  {
48  return (ran32State = 1664525 * ran32State + 1013904223);
49  }
50 };
51 
53 inline void srandom_number32(unsigned seed = 0)
54 {
55  if (!seed)
56  seed = get_next_seed();
57  ran32State = seed;
58 }
59 
64 {
65  typedef unsigned value_type;
66  mutable unsigned state;
67 
68  random_number32_r(unsigned seed = 0)
69  {
70  if (!seed)
71  seed = get_next_seed();
72  state = seed;
73  }
74 
76  inline value_type operator () () const
77  {
78  return (state = 1664525 * state + 1013904223);
79  }
80 };
81 
85 {
86  typedef double value_type;
87  random_number32 rnd32;
88 
89  random_uniform_fast(unsigned /*seed*/ = 0)
90  { }
91 
93  inline value_type operator () () const
94  {
95  return (double(rnd32()) * (0.5 / 0x80000000));
96  }
97 };
98 
104 {
105  typedef double value_type;
106 #ifdef STXXL_BOOST_RANDOM
107  typedef boost::minstd_rand base_generator_type;
108  base_generator_type generator;
109  boost::uniform_real<> uni_dist;
110  mutable boost::variate_generator<base_generator_type &, boost::uniform_real<> > uni;
111 
112  random_uniform_slow(unsigned seed = 0) : uni(generator, uni_dist)
113  {
114  if (!seed)
115  seed = get_next_seed();
116  uni.engine().seed(seed);
117  }
118 #else
119  mutable unsigned short state48[3];
120 
121  random_uniform_slow(unsigned seed = 0)
122  {
123  if (!seed)
124  seed = get_next_seed();
125  state48[0] = seed & 0xffff;
126  state48[1] = seed >> 16;
127  state48[2] = 42;
128  erand48(state48);
129  }
130 #endif
131 
133  inline value_type operator () () const
134  {
135 #ifdef STXXL_BOOST_RANDOM
136  return uni();
137 #else
138  return erand48(state48);
139 #endif
140  }
141 };
142 
144 template <class UniformRGen_ = random_uniform_fast>
146 {
147  typedef unsigned value_type;
148  UniformRGen_ uniform;
149 
150  random_number(unsigned seed = 0) : uniform(seed)
151  { }
152 
154  inline value_type operator () (value_type N) const
155  {
156  return static_cast<value_type>(uniform() * double(N));
157  }
158 };
159 
162 {
163  typedef stxxl::uint64 value_type;
164  random_uniform_slow uniform;
165 
166  random_number64(unsigned seed = 0) : uniform(seed)
167  { }
168 
170  inline value_type operator () () const
171  {
172  return static_cast<value_type>(uniform() * (18446744073709551616.));
173  }
174 };
175 
176 __STXXL_END_NAMESPACE
177 
178 #endif // !STXXL_RAND_HEADER
value_type operator()() const
Returns a random number from [0, 2^32)
Definition: rand.h:76
Fast uniform [0, 2^32) pseudo-random generator with period 2^32, random bits: 32. ...
Definition: rand.h:41
Fast uniform [0, 2^32) pseudo-random generator with period 2^32, random bits: 32. Reentrant variant o...
Definition: rand.h:63
value_type operator()() const
Returns a random number from [0, 2^32)
Definition: rand.h:46
value_type operator()(value_type N) const
Returns a random number from [0, N)
Definition: rand.h:154
value_type operator()() const
Returns a random number from [0.0, 1.0)
Definition: rand.h:133
Uniform [0, N) pseudo-random generator.
Definition: rand.h:145
value_type operator()() const
Returns a random number from [0.0, 1.0)
Definition: rand.h:93
Slow and precise uniform [0.0, 1.0) pseudo-random generator period: at least 2^48, random bits: at least 31.
Definition: rand.h:103
Fast uniform [0.0, 1.0) pseudo-random generator.
Definition: rand.h:84
value_type operator()() const
Returns a random number from [0, 2^64)
Definition: rand.h:170
Slow and precise uniform [0, 2^64) pseudo-random generator.
Definition: rand.h:161