Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
random.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/random/random.hh
11 /// @brief Random number generator system
12 /// @author Sergey Lyskov (Sergey.Lyskov@jhu.edu)
13 ///
14 /// @remarks
15 /// @li -
16 
17 
18 #ifndef INCLUDED_numeric_random_random_hh
19 #define INCLUDED_numeric_random_random_hh
20 
21 
22 // Unit headers
24 
25 // Package headers
27 
28 // Utility headers
31 
32 // C++ headers
33 #include <iostream>
34 #include <utility/vector1.hh> // need for template definition...
35 
36 
37 /// Currently supported RG types:
38 /// standard - build in C++ random generator
39 /// ran3 - old generator from previos version of rosetta
40 
41 #if (defined min) && (defined WIN32) // Workaround for MSVC and windows.h include which used #define min
42 #undef min
43 #endif
44 
45 #if (defined max) && (defined WIN32) // Workaround for MSVC and windows.h include which used #define max
46 #undef max
47 #endif
48 
49 
50 namespace numeric {
51 namespace random {
52 
53 class RandomGenerator;
54 
55 /// @brief Return the one-per-thread "singleton" random generator.
56 RandomGenerator & rg();
57 
58 /// @brief Generate a random number between 0 and 1. Threadsafe since each thread
59 /// uses its own random generator.
60 double uniform();
61 
62 /// @brief Generate a random number pulled from a standard normal -- i.e. mean of
63 /// zero and standard deviation of 1. Threadsafe since each thread uses its own
64 /// random generator
65 double gaussian();
66 
67 /// @brief Return a number uniformly drawn from the inclusive range between low
68 /// and high. Threadsafe since each thread uses its own random generator.
69 int random_range(int low, int high);
70 
71 
72 /// @brief Random number generator system
74 {
75 private:
76  // Private to force the instantiation of a RandomGenerator through the rg() function.
78 
79  // Do not make copies of RandomGenerators - pass by reference instead.
80  RandomGenerator( RandomGenerator const & ); // Unimplemented private -- do not use.
81 
82  friend RandomGenerator & rg();
83 
84 public:
85 
87 
88  /// Return from range [0, 1] (?) uniform random number
89  ///
90  /// The implementation of random_range leads me to believe this is
91  /// actually [0, 1), like most other random number generators. -IWD
92  double uniform();
93 
94  /// @brief Get Gaussian distribution random number
95  double gaussian();
96 
97  /// @brief Returns a random int in the range specified by the arguments
98  int random_range( int low, int high );
99 
100  /// @brief Return the seed used by this RNG.
101  int get_seed() const;
102 
103  /// @brief Set the seed and the generator type synchronously.
104  /// Currently the two supported generator types are "standard"
105  /// and "mt19937" with the latter being the recommended form.
106  void set_seed( std::string const & generator_type, int seed );
107 
108  /// @brief Return the seed used by this RNG.
109  void set_seed( int seed );
110 
111  void saveState(std::ostream & out);
112 
113  void restoreState(std::istream & in);
114 
115  /// @brief return a random element from a utility::vector1. What is
116  /// this function doing inside the RandomGenerator class?
117  template< class T >
118  T const &
120  {
121  assert( !v.empty() );
122  return v[ RandomGenerator::random_range( 1, v.size() ) ];
123  }
124 
125 public: // implement the boost Uniform Random Generator concept
126 
127  typedef double result_type;
128 
129  // Maybe this should call uniform() instead?
130  double operator()() { return gaussian(); }
131 
132  double min() const { return 0; }
133  double max() const { return 1; }
134 
135 private: // Fields
136 
137  //int seed_offset_; /// Our magic number goes there
138 
140 
141  /// data for Gaussian generation
144 
145 }; // RandomGenerator
146 
147 typedef utility::pointer::shared_ptr< RandomGenerator > RandomGeneratorOP;
148 
149 } // namespace random
150 } // namespace numeric
151 
152 
153 #endif // INCLUDED_numeric_random_random_HH
bool gaussian_iset_
data for Gaussian generation
Definition: random.hh:142
RandomGenerator & rg()
Return the one-per-thread "singleton" random generator.
Definition: random.cc:46
utility::pointer::shared_ptr< uniform_RG > uniform_RG_OP
Definition: uniform.fwd.hh:23
ReferenceCount base class – dispatch class.
Non-owning access smart pointer – dispatch class.
Random number generator system.
Definition: random.hh:73
void saveState(std::ostream &out)
Definition: random.cc:159
double gaussian()
Generate a random number pulled from a standard normal – i.e. mean of zero and standard deviation of...
Definition: random.cc:57
int random_range(int low, int high)
Return a number uniformly drawn from the inclusive range between low and high. Threadsafe since each ...
Definition: random.cc:58
Base class for reference-counted polymorphic classes.
void restoreState(std::istream &in)
Definition: random.cc:169
double gaussian()
Get Gaussian distribution random number.
Definition: random.cc:99
friend RandomGenerator & rg()
Return the one-per-thread "singleton" random generator.
Definition: random.cc:46
utility::pointer::shared_ptr< RandomGenerator > RandomGeneratorOP
Definition: random.hh:147
int get_seed() const
Return the seed used by this RNG.
Definition: random.cc:139
vector1: std::vector with 1-based indexing
int random_range(int low, int high)
Returns a random int in the range specified by the arguments.
Definition: random.cc:126
double uniform()
Generate a random number between 0 and 1. Threadsafe since each thread uses its own random generator...
Definition: random.cc:56
T const & random_element(utility::vector1< T > const &v)
return a random element from a utility::vector1. What is this function doing inside the RandomGenerat...
Definition: random.hh:119
void set_seed(std::string const &generator_type, int seed)
Set the seed and the generator type synchronously. Currently the two supported generator types are "s...
Definition: random.cc:145
Random number generator system.