BitmapToolkit Scol plugin
Random.h
Go to the documentation of this file.
1/*
2 * File: Random.h
3 * Project: DUtils library
4 * Author: Dorian Galvez-Lopez
5 * Date: April 2010, November 2011
6 * Description: manages pseudo-random numbers
7 * License: see the LICENSE.txt file
8 *
9 */
10
11#pragma once
12#ifndef __D_RANDOM__
13#define __D_RANDOM__
14
15#include <cstdlib>
16#include <vector>
17
18namespace DUtils {
19
21class Random
22{
23public:
25
26public:
30 static void SeedRand();
31
36 static void SeedRandOnce();
37
42 static void SeedRand(int seed);
43
49 static void SeedRandOnce(int seed);
50
55 template <class T>
56 static T RandomValue(){
57 return (T)rand()/(T)RAND_MAX;
58 }
59
66 template <class T>
67 static T RandomValue(T min, T max){
68 return Random::RandomValue<T>() * (max - min) + min;
69 }
70
77 static int RandomInt(int min, int max);
78
84 template <class T>
85 static T RandomGaussianValue(T mean, T sigma)
86 {
87 // Box-Muller transformation
88 T x1, x2, w, y1;
89
90 do {
91 x1 = (T)2. * RandomValue<T>() - (T)1.;
92 x2 = (T)2. * RandomValue<T>() - (T)1.;
93 w = x1 * x1 + x2 * x2;
94 } while ( w >= (T)1. || w == (T)0. );
95
96 w = sqrt( ((T)-2.0 * log( w ) ) / w );
97 y1 = x1 * w;
98
99 return( mean + y1 * sigma );
100 }
101
102private:
103
105 static bool m_already_seeded;
106
107};
108
109// ---------------------------------------------------------------------------
110
113{
114public:
115
121 UnrepeatedRandomizer(int min, int max);
123
129
135
141 int get();
142
149 inline bool empty() const { return m_values.empty(); }
150
155 inline unsigned int left() const { return m_values.size(); }
156
160 void reset();
161
162protected:
163
167 void createValues();
168
169protected:
170
172 int m_min;
174 int m_max;
175
177 std::vector<int> m_values;
178
179};
180
181}
182
183#endif
184
Provides pseudo-random numbers with no repetitions.
Definition Random.h:113
UnrepeatedRandomizer & operator=(const UnrepeatedRandomizer &rnd)
Definition Random.cpp:116
int m_max
Max of range of values.
Definition Random.h:174
int m_min
Min of range of values.
Definition Random.h:172
unsigned int left() const
Definition Random.h:155
std::vector< int > m_values
Available values.
Definition Random.h:177
Functions to generate pseudo-random numbers.
Definition Random.h:22
static T RandomValue(T min, T max)
Definition Random.h:67
static void SeedRandOnce()
Definition Random.cpp:24
static T RandomGaussianValue(T mean, T sigma)
Definition Random.h:85
static void SeedRand()
Definition Random.cpp:18
static int RandomInt(int min, int max)
Definition Random.cpp:47
static T RandomValue()
Definition Random.h:56