Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
random.cc
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 src/numeric/random/random.cc
11 /// @brief Random number generator system
12 /// @author Sergey Lyskov (Sergey.Lyskov@jhu.edu)
13 
14 // Unit headers
15 #include <numeric/random/random.hh>
16 
17 // Package headers
20 
21 // Utility headers
22 #include <utility/exit.hh>
23 
24 // C++ 11 Compatibility
26 
27 // C++ headers
28 #include <iostream>
29 #include <cmath>
30 
31 // C++ 11 support
33 
34 
35 namespace numeric {
36 namespace random {
37 
38 #ifdef CXX11
39 // Use a "thread_local" declaration to ensure that the each thread has its own
40 // random_generator pointer.
42 #else
44 #endif
45 
47 {
48  // If you want thread saftety mechanisms but don't want to use C++11 (foldit?) then
49  // you'll want to edit this logic.
50  if ( random_generator == 0 ) {
52  }
53  return *random_generator;
54 }
55 
56 double uniform() { return rg().uniform(); }
57 double gaussian() { return rg().gaussian(); }
58 int random_range(int low, int high) { return rg().random_range(low, high); }
59 
60 
61 using namespace std;
62 
63 /// uniform_RG factory
65 createRG(string const & type )
66 {
67  if ( type == "standard" ) return uniform_RG_OP( new standard_RG() );
68  //if( type == "ran3" ) return new ran3_RG();
69  if ( type == "mt19937" ) return uniform_RG_OP( new mt19937_RG() );
70 
71  utility_exit_with_message("Unknown random number generator type: " + type);
72  return 0;
73 }
74 
76  gaussian_iset_( true ),
77  gaussian_gset_( 0 )
78 {}
79 
81 {}
82 
83 
84 double
86  return generator_->getRandom();
87 }
88 
89 /// @brief
90 /// SL: this is function is ported from old Rosetta++.
91 ///
92 /// Returns a gaussian random number (normally distributed deviate with
93 /// zero mean and unit variance) using ran3 as a source of uniform deviates.
94 /// Always call with the same idum
95 ///
96 /// @references Numerical Recipes, section 7.2, a.k.a. "GASDEV"
97 ///
98 /// @author JJG 4/01
100 {
101  double rgaussian; // Return value
102  if ( gaussian_iset_ ) {
103  double v1, v2, rsq, fac;
104  do {
105  v1 = 2.0f * uniform() - 1.0f;
106  v2 = 2.0f * uniform() - 1.0f;
107  rsq = ( v1 * v1 ) + ( v2 * v2 );
108  } while ( rsq >= 1.0 || rsq == 0.0 );
109  fac = std::sqrt(-(2.0*std::log(rsq)/rsq));
110  gaussian_gset_ = v1*fac;
111  rgaussian = v2*fac;
112  gaussian_iset_ = false;
113  } else {
114  rgaussian = gaussian_gset_;
115  gaussian_iset_ = true;
116  }
117  // std::cout << "NR Gaussian: " << gaussian << std::endl;
118  return rgaussian;
119 }
120 
121 
122 /// @brief Returns a random int in the range specified by the arguments,
123 /// with both enpoints being included in the possible output.
124 ///
125 /// @author XA
126 int RandomGenerator::random_range(int low, int high)
127 {
128  if ( low > high ) {
129  int temp;
130  temp = low;
131  low = high;
132  high = temp;
133  }
134 
135  int const range( high - low + 1 );
136  return ( static_cast< int >( range * numeric::random::uniform() ) + low );
137 }
138 
140 {
141  return generator_->getSeed();
142 }
143 
144 void
146  std::string const & generator_type,
147  int seed
148 )
149 {
150  generator_ = createRG( generator_type );
151  generator_->setSeed( seed );
152 }
153 
154 
155 void RandomGenerator::set_seed( int seed ) {
156  generator_->setSeed(seed);
157 }
158 
159 void RandomGenerator::saveState(std::ostream & out)
160 {
161  //out << " " << seed_offset;
162  out << " " << gaussian_iset_;
163  // Cast raw bits to int for perfect precision:
164  out << " " << *((uint64_t*) &gaussian_gset_);
165  generator_->saveState(out);
166  out << "\n";
167 }
168 
169 void RandomGenerator::restoreState(std::istream & in)
170 {
171  in >> gaussian_iset_;
172  // Cast raw bits from int for perfect precision:
173  uint64_t gset_bits = 0;
174  in >> gset_bits;
175  gaussian_gset_ = *((double*) &gset_bits);
176  generator_->restoreState(in);
177 }
178 
179 } // namespace random
180 } // namespace numeric
#define utility_exit_with_message(m)
Exit with file + line + message.
Definition: exit.hh:47
bool gaussian_iset_
data for Gaussian generation
Definition: random.hh:142
#define THREAD_LOCAL
uniform_RG_OP createRG(string const &type)
uniform_RG factory
Definition: random.cc:65
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
Random number generator system.
Definition: random.hh:73
void saveState(std::ostream &out)
Definition: random.cc:159
Generator based on rand() < clib > function.
Definition: uniform.hh:61
Random number generator system.
double log(double x, double base)
Computes log(x) in the given base.
Definition: util.hh:41
double gaussian()
Generate a random number pulled from a standard normal – i.e. mean of zero and standard deviation of...
Definition: random.cc:57
Uniform random number generator.
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
File to provide backwards compatibility for the thread_local keyword.
void restoreState(std::istream &in)
Definition: random.cc:169
double gaussian()
Get Gaussian distribution random number.
Definition: random.cc:99
Program exit functions and macros.
UINT64 uint64_t
Definition: types.hh:27
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
Mersenne Twister 19937 random number generator.
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
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
static RandomGeneratorOP random_generator(0)