Berkeley SfM
Public Member Functions | Static Public Member Functions | List of all members
bsfm::math::RandomGenerator Class Reference

#include <random_generator.h>

Public Member Functions

 RandomGenerator (unsigned long seed)
 
int Integer ()
 
int IntegerUniform (int max)
 
int IntegerUniform (int min, int max)
 
void Integers (size_t count, std::vector< int > *integers)
 
void IntegersUniform (size_t count, int min, int max, std::vector< int > *integers)
 
double Double ()
 
double DoubleUniform (double min, double max)
 
double DoubleGaussian (double mean, double stddev)
 
void Doubles (size_t count, std::vector< double > *doubles)
 
void DoublesUniform (size_t count, double min, double max, std::vector< double > *doubles)
 
void DoublesGaussian (size_t count, double mean, double stddev, std::vector< double > *doubles)
 

Static Public Member Functions

static unsigned long Seed ()
 

Detailed Description

Definition at line 52 of file random_generator.h.

Constructor & Destructor Documentation

bsfm::math::RandomGenerator::RandomGenerator ( unsigned long  seed)
explicit

Definition at line 43 of file random_generator.cpp.

43  {
44  srand(seed);
45 }

Member Function Documentation

double bsfm::math::RandomGenerator::Double ( )

Definition at line 109 of file random_generator.cpp.

109  {
110  return static_cast<double>(Integer()) / static_cast<double>(RAND_MAX);
111 }
double bsfm::math::RandomGenerator::DoubleGaussian ( double  mean,
double  stddev 
)

Definition at line 125 of file random_generator.cpp.

125  {
126  // Use the box-muller transform to approximate a normal distribution:
127  // https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
128  // u must be \in (0, 1], and v must be \in [0, 1).
129  double u = 1.0 - Double();
130  double v = Double();
131  double z = sqrt(-2.0 * log(u)) * cos(2.0 * M_PI * v);
132 
133  return mean + stddev * z;
134 }
void bsfm::math::RandomGenerator::Doubles ( size_t  count,
std::vector< double > *  doubles 
)

Definition at line 136 of file random_generator.cpp.

136  {
137  if (doubles == nullptr) {
138  return;
139  }
140 
141  for (size_t i = 0; i < count; ++i) {
142  doubles->push_back(Double());
143  }
144 }
void bsfm::math::RandomGenerator::DoublesGaussian ( size_t  count,
double  mean,
double  stddev,
std::vector< double > *  doubles 
)

Definition at line 157 of file random_generator.cpp.

158  {
159  if (doubles == nullptr) {
160  return;
161  }
162 
163  for (size_t i = 0; i < count; ++i) {
164  doubles->push_back(DoubleGaussian(mean, stddev));
165  }
166 }
double DoubleGaussian(double mean, double stddev)
void bsfm::math::RandomGenerator::DoublesUniform ( size_t  count,
double  min,
double  max,
std::vector< double > *  doubles 
)

Definition at line 146 of file random_generator.cpp.

147  {
148  if (doubles == nullptr) {
149  return;
150  }
151 
152  for (size_t i = 0; i < count; ++i) {
153  doubles->push_back(DoubleUniform(min, max));
154  }
155 }
double DoubleUniform(double min, double max)
double bsfm::math::RandomGenerator::DoubleUniform ( double  min,
double  max 
)

Definition at line 113 of file random_generator.cpp.

113  {
114  if (min >= max) {
115  LOG(WARNING) << "min >= max. Returning min.";
116  // Eat a random number anyways and return min.
117  Integer();
118  return min;
119  }
120 
121  double coefficient = (max - min) / static_cast<double>(RAND_MAX);
122  return min + static_cast<double>(Integer()) * coefficient;
123 }
int bsfm::math::RandomGenerator::Integer ( )

Definition at line 60 of file random_generator.cpp.

60  {
61  // TODO(eanelson: Use a threadsafe rng.
62  return rand();
63 }
void bsfm::math::RandomGenerator::Integers ( size_t  count,
std::vector< int > *  integers 
)

Definition at line 88 of file random_generator.cpp.

88  {
89  if (integers == nullptr) {
90  return;
91  }
92 
93  for (size_t i = 0; i < count; ++i) {
94  integers->push_back(Integer());
95  }
96 }
void bsfm::math::RandomGenerator::IntegersUniform ( size_t  count,
int  min,
int  max,
std::vector< int > *  integers 
)

Definition at line 98 of file random_generator.cpp.

99  {
100  if (integers == nullptr) {
101  return;
102  }
103 
104  for (size_t i = 0; i < count; ++i) {
105  integers->push_back(IntegerUniform(min, max));
106  }
107 }
int bsfm::math::RandomGenerator::IntegerUniform ( int  max)

Definition at line 66 of file random_generator.cpp.

66  {
67  if (max <= 0) {
68  LOG(WARNING) << "max <= 0. Returning 0.";
69  // Eat a random number anyways and return 0.
70  Integer();
71  return 0;
72  }
73 
74  return Integer() % static_cast<int>(max + 1);
75 }
int bsfm::math::RandomGenerator::IntegerUniform ( int  min,
int  max 
)

Definition at line 77 of file random_generator.cpp.

77  {
78  if (min >= max) {
79  LOG(WARNING) << "min >= max. Returning min.";
80  // Eat a random number anyways and return min.
81  Integer();
82  return min;
83  }
84 
85  return min + (Integer() % static_cast<int>(max - min + 1));
86 }
unsigned long bsfm::math::RandomGenerator::Seed ( )
static

Definition at line 47 of file random_generator.cpp.

47  {
48  // Hash from: http://burtleburtle.net/bob/hash/doobs.html
49  // TODO(eanelson): Update this to read a seed from /dev/urandom instead.
50  unsigned long a = clock();
51  unsigned long b = time(NULL);
52  unsigned long c = getpid();
53  a-=b; a-=c; a^=(c >> 13); b-=c; b-=a; b^=(a << 8); c-=a; c-=b; c^=(b >> 13);
54  a-=b; a-=c; a^=(c >> 12); b-=c; b-=a; b^=(a << 16); c-=a; c-=b; c^=(b >> 5);
55  a-=b; a-=c; a^=(c >> 3); b-=c; b-=a; b^=(a << 10); c-=a; c-=b; c^=(b >> 15);
56  return c;
57 }

The documentation for this class was generated from the following files: