SlideShare a Scribd company logo
Random number generation (in C++)
- past, present and potential future
Pattabi Raman
pattabi@numericalsolution.co.uk
http://www.numericalsolution.co.uk/random-numbers/
What is Random?
It represents an event or entity, which cannot be determined but only
described probabilistically,
For example:
• Falling rain drops which hit the ground in random pattern,
• Distribution of stars with in the universe is a random, and
• Babies cry in random and so on
www.numericalsolution.co.uk/random-numbers/ 2ACCU 2014
What are the use of Random Numbers?
They are mainly used in:
• Simulations be it a numerical calculation or a cartoon game,
• Binning analog data in channel format,
• Testing a product and so on.
www.numericalsolution.co.uk/random-numbers/ 3ACCU 2014
What is the plan of this presentation?
• Historic evolution of random numbers and their applications,
• Modern development and implementation in C++, and beyond.
www.numericalsolution.co.uk/random-numbers/ 4ACCU 2014
How did random numbers and their applications evolve?
www.numericalsolution.co.uk/random-numbers/ 5ACCU 2014
Since pre-historic period, random numbers were generated from dice for gambling
www.numericalsolution.co.uk/random-numbers/ 6ACCU 2014
In bronze age gambling became unethical
Random numbers from dice
Made Kings to give up their crowns to opponents
www.numericalsolution.co.uk/random-numbers/ 7ACCU 2014
In 300 BC, Prof Chanakya of Takshashila University
prescribed laws and taxations to regulate gambling.
www.numericalsolution.co.uk/random-numbers/ 8ACCU 2014
In 1650 Blaise Pascal, who introduced the computer in the form of his mechanical calculator
www.numericalsolution.co.uk/random-numbers/ 9ACCU 2014
Designed perpetual motion experiment
www.numericalsolution.co.uk/random-numbers/ 10ACCU 2014
That lead the way to the Roulette
www.numericalsolution.co.uk/random-numbers/ 11ACCU 2014
Casino de Monte-Carlo
Monaco - MC 98000
www.numericalsolution.co.uk/random-numbers/ 12ACCU 2014
Casino de Monte-Carlo
Monaco - MC 98000
www.numericalsolution.co.uk/random-numbers/ 13ACCU 2014
In 1930
FERMIAC The Monte Carlo trolley
Random number generator
statistical sampling techniques
www.numericalsolution.co.uk/random-numbers/ 14ACCU 2014
While playing solitaire during his recovery from a surgery, he had
thought about playing hundreds of games to estimate statistically
the probability of a successful outcome
This lead to the idea of Monte Carlo Method!
Stan Ulam
In 1940
www.numericalsolution.co.uk/random-numbers/ 15ACCU 2014
ENIAC - first electronic general-purpose computer
Used Monte Carlo Method and
employed random numbers to solve
complicated problems.
Obtained random numbers from:
 nuclear radioactivity;
 voltage fluctuation;
 Solar flare and
stored in punch cards.
But reading from punch card was very slow!
John von Neumann
www.numericalsolution.co.uk/random-numbers/ 16ACCU 2014
ENIAC - first electronic general-purpose computer
Used Monte Carlo Method and
employed random numbers to solve
complicated problems.
So, he developed pseudo-random numbers,
using the 13th century mid-square method
Let, Seed 𝒙 𝟎 = 0.7891, then
𝒙 𝟎
𝟐
= 0.62 2678 81
⇒ 𝒙 𝟏 = 0.2678
𝒙 𝟏
𝟐
= 0.07 1716 84
⇒ 𝒙 𝟐 = 0.1716
𝒙 𝟐
𝟐
= 0.02 9446 56
⇒ 𝒙 𝟑 = 0.9446
John von Neumann
"Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin."
www.numericalsolution.co.uk/random-numbers/ 17ACCU 2014
John von Neumann
He applied Monte Carlo Methods and
www.numericalsolution.co.uk/random-numbers/ 18ACCU 2014
John von Neumann
He applied Monte Carlo Methods and
www.numericalsolution.co.uk/random-numbers/ 19ACCU 2014
John von Neumann
He applied Monte Carlo Methods and
www.numericalsolution.co.uk/random-numbers/ 20ACCU 2014
John von Neumann
He applied Monte Carlo Methods and
replicators
www.numericalsolution.co.uk/random-numbers/ 21ACCU 2014
John von Neumann
He applied Monte Carlo Methods and
replicators
www.numericalsolution.co.uk/random-numbers/ 22ACCU 2014
In 1957
Fortran (Formula Translating System)
Multiplicative Congruential Generator (MCG)
𝑋 𝑛 = (a𝑋 𝑛−1) mod m
RANDOM = MOD(A*SEED,M)
PRINT*, RANDOM
SEED = RANDOM
Linear congruential generator
𝑋 𝑛 = (a𝑋 𝑛−1 + b) mod m
www.numericalsolution.co.uk/random-numbers/ 23ACCU 2014
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
static const unsigned long int a = 1103515245;
static const unsigned short b = 12345;
next = next * a + b;
return (unsigned int)(next/65536) % 32768;;
}
void srand(unsigned int seed)
{
next = seed;
}
In 1972 C
www.numericalsolution.co.uk/random-numbers/ 24ACCU 2014
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
static const unsigned long int a = 1103515245;
static const unsigned short b = 12345;
next = next * a + b;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed;
}
In 1972 C
www.numericalsolution.co.uk/random-numbers/ 25ACCU 2014
#include <cstdlib>
#include <cstdio>
#include <ctime>
int main()
{
srand(time(NULL));
printf("Random numbers:n");
float random = 0;
for (int i = 0; i < 10; ++i) {
random = (float) rand()/RAND_MAX;
printf("%fn", random);
}
}
Random numbers:
0.003143
0.569353
0.033021
0.545427
0.627430
0.035096
0.817377
0.055635
0.282266
0.187628
www.numericalsolution.co.uk/random-numbers/ 26ACCU 2014
 BCPL
 1972 C (proximity to hardware)
1967 Simula (OOP)
 ALGOL
 Fortran
 Speedcoding
1983 C++
C with Classes (of Simula)
Machine code (100011 00011)
 Assembly language (mov ax,@data)
www.numericalsolution.co.uk/random-numbers/ 27ACCU 2014
int rand (void);
void srand (unsigned int seed);
#define RAND_MAX 32767
std::random_shuffle (data.begin(), data.end());
Initial data is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Shuffled data is {6, 10, 5, 2, 3, 7, 9, 8, 1, 4}
In 1983 C++
www.numericalsolution.co.uk/random-numbers/ 28ACCU 2014
ACCU 2014 www.numericalsolution.co.uk/random-numbers/ 29
Random Numbers - Numberphile
http://www.youtube.com/watch?v=SxP30euw3-0
www.numericalsolution.co.uk/random-numbers/ 30ACCU 2014
www.numericalsolution.co.uk/random-numbers/ 31ACCU 2014
ACCU 2014 www.numericalsolution.co.uk/random-numbers/ 32
In 2011 C++ 11
<random> (26.5 Random number generation N3797)
Random = Distribution(Engine)
www.numericalsolution.co.uk/random-numbers/ 33ACCU 2014
In 2011 C++ 11
<random> (26.5 Random number generation N3797)
Random = Distribution(Engine)
Engines:
linear_congruential_engine
subtract_with_carry_engine
mersenne_twister_engine
Engines Adaptors:
discard_block_engine
independent_bits_engine
shuffle_order_engine
True random number generator:
random_device
Distributions:
Uniform distributions
Normal distributions
Bernoulli distributions
Rate-based distributions
Piecewise distributions
Canonical numbers
www.numericalsolution.co.uk/random-numbers/ 34ACCU 2014
linear_congruential_engine
𝑋 𝑛 = (a𝑋 𝑛−1 + b) mod m
minstd_rand0: a = 16807; b = 0; m = 2, 147, 483, 647
minstd_rand: a = 48271; b = 0; m = 2, 147, 483, 647
www.numericalsolution.co.uk/random-numbers/ 35ACCU 2014
linear_congruential_engine
𝑋 𝑛 = (a𝑋 𝑛−1 + b) mod m
minstd_rand0: a = 16807; b = 0; m = 2, 147, 483, 647
minstd_rand: a = 48271; b = 0; m = 2, 147, 483, 647
www.numericalsolution.co.uk/random-numbers/ 36ACCU 2014
subtract_with_carry_engine
𝑋n = (𝑋n−S - 𝑋n−R - 𝑐𝑦n−1) mod m
where, c𝑦n = (𝑋n−S - 𝑋n−R - 𝑐𝑦n−1 < 0) ? 1 : 0;
𝑋 𝑛 = (a𝑋 𝑛−1 + b) mod m = f (𝑋 𝑛−1)Linear Congruential algorithm =>
Lagged Fibonacci algorithm => f (𝑋 𝑛−1, 𝑋 𝑛−2) => f (𝑋 𝑛−𝑆, 𝑋 𝑛−𝑅); where, S < R < 0
1991 George Marsaglia and Arif Zaman
ranlux24_base: 24-bit number S = 10; R = 24;
ranlux48_base: 48-bit number S = 5; R = 12;
www.numericalsolution.co.uk/random-numbers/ 37ACCU 2014
mersenne_twister_engine
Period length = Mersenne prime = 219937−1
MT19937 uses a 32-bit word length
MT19937-64 uses a 64-bit word length
1997Makoto Matsumoto and Takuji Nishimura
“Twisted Generalized Feedback Shift Register”
www.numericalsolution.co.uk/random-numbers/ 38ACCU 2014
random_device
Generates random numbers from hardware where available
CRTIMP2_PURE unsigned int __CLRCALL_PURE_OR_CDECL _Random_device();
unsigned int operator()()
{
return (_Random_device());
}
std::random_device rd;
std::default_random_engine e1(rd());
www.numericalsolution.co.uk/random-numbers/ 39ACCU 2014
void my_random_generator::check_mt19937()
{
mt19937 engine;
static long long random_number = 0;
for(int i =0; i != 9999; ++i) engine();
random_number = engine();
// If the implementation is correct then the 10000th consecutive
// invocation of a default-constructed object of type mt19937
// shall produce the value 4,123,659,995, ref. C++11 ISO statement.
if(random_number == 4123659995)
cout << "n Note:nt The pseudorandom number generatornt "
<< "Mersenne twister: MT19937 has been testednt and "
<< "it shows it is implemented properly.n";
else
cout << "nWarning:nt "
<< "The pseudorandom number generatornt "
<< "Mersenne twister: MT19937 has been "
<< "testednt and it shows it is NOT "
<< "implemented properly.n";
}
Required behaviour: The 10000th consecutive invocation of a default-constructed object of type mt19937 shall produce the value 4123659995.
www.numericalsolution.co.uk/random-numbers/ 40ACCU 2014
auto dist = bind(uniform_real_distribution<double>{0.0, 1.0}, default_random_engine{});
Random = dist();
// A Uniform Distribution based on default_random_engine:
// Using Bind function:
www.numericalsolution.co.uk/random-numbers/ 41ACCU 2014
class uniform_dist {
public:
double operator()() { return uniform(engine); }
uniform_dist() :uniform{ 0.0, 1.0 } {}
void discard(unsigned long long z) { engine.discard(z); }
void discard_distribution(unsigned long long z)
{
for (auto i = z; i != 0; --i)
uniform(engine);
}
uniform_dist(double low, double high) : uniform(low, high) {}
private:
default_random_engine engine;
uniform_real_distribution <double> uniform;
};
www.numericalsolution.co.uk/random-numbers/ 42ACCU 2014
// A Normal distribution based on
// Mersenne Twister engine:
class normal_dist {
public:
double operator()() { return normal(engine); }
normal_dist() :normal {0.0, 1.0} {}
void discard(unsigned long long z) { engine.discard(z); }
void discard_distribution(unsigned long long z)
{
for (auto i = z; i != 0; --i)
normal(engine);
}
normal_dist(double mean, double std_dev) : normal( mean, std_dev ) {}
private:
mt19937 engine;
normal_distribution<double> normal;
};
www.numericalsolution.co.uk/random-numbers/ 43ACCU 2014
0
0.0005
0.001
0.0015
0.002
0.0025
0.003
0.0035
0.004
0.0045
-8 -6 -4 -2 0 2 4 6 8
Φ(X)
X
2 = 1
= 0
2 = 1
= 0
Normal distribution
www.numericalsolution.co.uk/random-numbers/ 44ACCU 2014
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
-8 -6 -4 -2 0 2 4 6 8
Σ(X)
X
CDF
= 0
2 = 1
0
0.0005
0.001
0.0015
0.002
0.0025
0.003
0.0035
0.004
0.0045
-8 -6 -4 -2 0 2 4 6 8
(X)
X
2 = 1
= 0
2 = 1
= 0
Probability normal distribution
Cumulative normal distribution
www.numericalsolution.co.uk/random-numbers/ 45ACCU 2014
0
0.2
0.4
0.6
0.8
1
1.2
-8 -6 -4 -2 0 2 4 6 8
Σ(X)
X
CDF
= 0
2 = 1
0
0.0005
0.001
0.0015
0.002
0.0025
0.003
0.0035
0.004
0.0045
-8 -6 -4 -2 0 2 4 6 8
(X)
X
2 = 1
= 0
2 = 1
= 0
Probability normal distribution
Cumulative normal distribution
www.numericalsolution.co.uk/random-numbers/ 46ACCU 2014
0
0.2
0.4
0.6
0.8
1
1.2
-8 -6 -4 -2 0 2 4 6 8
Σ(X)
X
CDF
= 0
2 = 1
0
0.0005
0.001
0.0015
0.002
0.0025
0.003
0.0035
0.004
0.0045
-8 -6 -4 -2 0 2 4 6 8
(X)
X
2 = 1
= 0
2 = 1
= 0
Probability normal distribution
Cumulative normal distribution
≈ Uniform distribution
www.numericalsolution.co.uk/random-numbers/ 47ACCU 2014
Pseudo-code algorithm for rational approximation
The algorithm below assumes p is the input and x is the output.
Coefficients in rational approximations.
a(1) <- -3.969683028665376e+01
a(2) <- 2.209460984245205e+02
a(3) <- -2.759285104469687e+02
a(4) <- 1.383577518672690e+02
a(5) <- -3.066479806614716e+01
a(6) <- 2.506628277459239e+00
b(1) <- -5.447609879822406e+01
b(2) <- 1.615858368580409e+02
b(3) <- -1.556989798598866e+02
b(4) <- 6.680131188771972e+01
b(5) <- -1.328068155288572e+01
c(1) <- -7.784894002430293e-03
c(2) <- -3.223964580411365e-01
c(3) <- -2.400758277161838e+00
c(4) <- -2.549732539343734e+00
c(5) <- 4.374664141464968e+00
c(6) <- 2.938163982698783e+00
d(1) <- 7.784695709041462e-03
d(2) <- 3.224671290700398e-01
d(3) <- 2.445134137142996e+00
d(4) <- 3.754408661907416e+00
Define break-points.
p_low <- 0.02425
p_high <- 1 - p_low
Rational approximation for lower region.
if 0 < p < p_low
q <- sqrt(-2*log(p))
x <- (((((c(1)*q+c(2))*q+c(3))*q+c(4))*q+c(5))*q+c(6)) /
((((d(1)*q+d(2))*q+d(3))*q+d(4))*q+1)
endif
Rational approximation for central region.
if p_low <= p <= p_high
q <- p - 0.5
r <- q*q
x <- (((((a(1)*r+a(2))*r+a(3))*r+a(4))*r+a(5))*r+a(6))*q /
(((((b(1)*r+b(2))*r+b(3))*r+b(4))*r+b(5))*r+1)
endif
Rational approximation for upper region.
if p_high < p < 1
q <- sqrt(-2*log(1-p))
x <- -(((((c(1)*q+c(2))*q+c(3))*q+c(4))*q+c(5))*q+c(6)) /
((((d(1)*q+d(2))*q+d(3))*q+d(4))*q+1)
endif
http://home.online.no/~pjacklam/notes/invnorm/index.html
www.numericalsolution.co.uk/random-numbers/ 48ACCU 2014
Please avoid coding random number generators or distributions!
Make use of your compliers and
www.numericalsolution.co.uk/random-numbers/ 49ACCU 2014
www.numericalsolution.co.uk/random-numbers/ 50ACCU 2014
Vt = Vt-dt( 1 + r.dt + sigma.phi(). 𝒅𝒕),
for(i = 1; i < T; ++i) {
dX = phi()*sqrt_dt;
dS = S*(r_dt + sigma*dX);
S += dS;
data[i] = S;
}
www.numericalsolution.co.uk/random-numbers/ 51ACCU 2014
0
1000
2000
3000
4000
5000
6000
0 1008 2016 3024 4032 5040 6048
1 Mar 1 May 1 Jul 1 Nov 31 Dec1 Jan
Your realistic expectation
1 Sep
Houseprice(inhundreds)
Active hours (excludes holiday hours)
www.numericalsolution.co.uk/random-numbers/ 52ACCU 2014
www.numericalsolution.co.uk/random-numbers/ 53ACCU 2014
0
1
2
3
4
5
6
0 1 2 3 4 5 6 7
SpeedupS(N)
Number of threads (N)
without phi()
with phi()
Amdahl’s law => 𝑃 =
1−𝑆 𝑁
𝑆 1−𝑁
50
60
70
80
90
100
110
1 2 3 4 5 6 7
ParallelportionP(N)in%
Number of threads (N)
with phi()
without phi()
www.numericalsolution.co.uk/random-numbers/ 54ACCU 2014
65
70
75
80
85
90
95
0 1 2 3 4 5
%ofParallelcode
<random> engine number
uniform_int average
uniform_real average
normal_distribution
average
<random>
engine numbers:
1. minstd_rand0
2. mt19937
3. mt19937_64
4. knuth_b
5. ranlux24_base
(Dead Lock with Normal
Distribution)
6. ranlux24 (Dead Lock
with all!)
<random> engine
uniform_int uniform_real normal_distribution
average stdev average stdev average stdev
1. Linear congruential:
minstd_rand0
88.00 2.67 91.89 2.97 79.28 3.60
2. Mersenne twister:
mt19937
91.18 2.34 88.67 3.47 79.60 2.61
3. Mersenne twister:
mt19937_64
90.68 2.27 87.73 3.10 76.38 3.04
4. Shuffle order:
knuth_b
83.77 4.29 86.75 6.12 70.45 5.71
5. Subtract with carry:
ranlux24_base
88.09 2.14 83.62 4.74 Dead Lock!
6. Discard block:
ranlux24
Dead Lock! Dead Lock! Dead Lock!
www.numericalsolution.co.uk/random-numbers/ 55ACCU 2014
www.numericalsolution.co.uk/random-numbers/ 56ACCU 2014
Thanks to:
Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren
Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more
www.numericalsolution.co.uk/random-numbers/ 57ACCU 2014
template<class UIntType, size_t w, ...>
class mersenne_twister_engine
{
public:
....
....
....
explicit mersenne_twister_engine(result_type value = default_seed);
template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
void seed(result_type value = default_seed);
template<class Sseq> void seed(Sseq& q);
// generating functions
result_type operator()() const; // if const method then it would be safe for concurrency.
void discard(unsigned long long z);
};
Thanks to:
Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren
Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more
www.numericalsolution.co.uk/random-numbers/ 58ACCU 2014
template<class UIntType, size_t w, ...>
class mersenne_twister_engine
{
public:
....
....
....
explicit mersenne_twister_engine(result_type value = default_seed);
template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
void seed(result_type value = default_seed);
template<class Sseq> void seed(Sseq& q);
// generating functions
result_type operator()() const; // but it is not const method.
void discard(unsigned long long z);
};
Thanks to:
Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren
Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more
www.numericalsolution.co.uk/random-numbers/ 59ACCU 2014
template<class UIntType, size_t w, ...>
class mersenne_twister_engine
{
public:
....
....
....
explicit mersenne_twister_engine(result_type value = default_seed);
template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
void seed(result_type value = default_seed);
template<class Sseq> void seed(Sseq& q);
// generating functions
result_type operator()();
void discard(unsigned long long z);
};
// As it is not const method, it is not safe for concurrency!
Thanks to:
Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren
Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more
www.numericalsolution.co.uk/random-numbers/ 60ACCU 2014
• Declare random number generator as thread_local, then each thread can have independent copy of the random object.
• But, it will provide same set of numbers in all threads, so that is multiplication of same data, therefore, that cannot add to statistics!
Thanks to:
Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren
Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more
www.numericalsolution.co.uk/random-numbers/ 61ACCU 2014
• Construct independent engines for each thread and set different range!
// A Normal distribution based on
// Mersenne Twister engine:
class normal_dist {
public:
double operator()() { return normal(engine); }
normal_dist() :normal {0.0, 1.0} {}
void discard(unsigned long long z) { engine.discard(z); }
void discard_distribution(unsigned long long z)
{
for (auto i = z; i != 0; --i)
normal(engine);
}
normal_dist(double mean, double std_dev) : normal( mean, std_dev ) {}
private:
mt19937 engine;
normal_distribution<double> normal;
};
www.numericalsolution.co.uk/random-numbers/ 62ACCU 2014
3.03
1.99
0.95
3.24
2.20
0.98
0.00 0.50 1.00 1.50 2.00 2.50 3.00 3.50 4.00
NO DISCARD
DISCARD IN ENGINE
DISCARD IN DISTRIBUTION
Speed Up (number of threads = 4)
Normal distribution Uniform distribution
→ 𝐼𝑡 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑖𝑒𝑠
𝑡ℎ𝑒 𝑑𝑎𝑡𝑎 𝑛𝑜𝑡
𝑖𝑛𝑐𝑟𝑒𝑎𝑠𝑖𝑛𝑔
𝑡ℎ𝑒 𝑠𝑡𝑎𝑡𝑖𝑠𝑡𝑖𝑐𝑠
www.numericalsolution.co.uk/random-numbers/ 63ACCU 2014
Complexity of ‘discard(number)’, i.e., performance time should be reduced, so number theoreticians can help!
Quasi-Random Numbers
www.numericalsolution.co.uk/random-numbers/ 64ACCU 2014
• What are they?
• How do they differ from Pseudo-random numbers?
• Where do they help?
Quasi-Random Numbers
www.numericalsolution.co.uk/random-numbers/ 65ACCU 2014
Quasi-Random Numbers
www.numericalsolution.co.uk/random-numbers/ 66ACCU 2014
Quasi-Random Numbers
www.numericalsolution.co.uk/random-numbers/ 67ACCU 2014
Quasi-Random Numbers
www.numericalsolution.co.uk/random-numbers/ 68ACCU 2014
Quasi-Random Numbers
www.numericalsolution.co.uk/random-numbers/ 69ACCU 2014
Quasi-Random Numbers
www.numericalsolution.co.uk/random-numbers/ 70ACCU 2014
Quasi-Random Numbers
Low-discrepancy sequence
Pseudo-Random Numbers
𝜖 ∝ 𝑐 𝑑
(𝑙𝑛 𝑁) 𝑑
𝑵
d is dimension, i.e.,
d numbers of random
numbers were estimated
per iteration.
𝜖 ∝
1
𝑵
N is number of iterations.
Halton
Niederreiter-Xing
Sobol
Linear congruential
Subtract with carry
Mersenne twister
www.numericalsolution.co.uk/random-numbers/ 71ACCU 2014
Quasi-Random Numbers
Low-discrepancy sequence
Pseudo-Random Numbers
𝜖 ∝ 𝑐 𝑑
(𝑙𝑛 𝑁) 𝑑
𝑵
d is dimension, i.e.,
d numbers of random
numbers were estimated
per iteration.
𝜖 ∝
1
𝑵
N is number of iterations.
Halton
Niederreiter-Xing
Sobol
–it uses primitive polynomial
based bitwise arithmetic
without carry - XOR
Linear congruential
Subtract with carry
Mersenne twister
y = f(x, random()) + g(x, random()); → 𝑒𝑞𝑢𝑎𝑡𝑖𝑜𝑛 1
P = f(Q, random()) + g(R, random()) + h(T, random()); → 𝑒𝑞𝑢𝑎𝑡𝑖𝑜𝑛 2
www.numericalsolution.co.uk/random-numbers/ 72ACCU 2014
ACCU 2014 www.numericalsolution.co.uk/random-numbers/ 73
ACCU 2014 www.numericalsolution.co.uk/random-numbers/ 74
“Most programmers will soon discover that the rand() function
is completely inadequate because it can only generate
a single stream of random numbers.
Most games need multiple discrete streams of random numbers”.
www.numericalsolution.co.uk/random-numbers/ 75ACCU 2014
Potential candidates for C++17:
• Dimensionality to random number generations;
• Low-discrepancy sequence (Sobol numbers);
int pick_a_number( int from, int thru )
{
static std::uniform_int_distribution<> d{};
using parm_t = decltype(d)::param_type;
return d( global_urng(), parm_t{from, thru} );
}
double pick_a_number( double from, double upto )
{
static std::uniform_real_distribution<> d{};
using parm_t = decltype(d)::param_type;
return d( global_urng(), parm_t{from, upto} );
}
Three <random>-related Proposals, v2
Document #: WG21 N3742
Date: 2013-08-30
Revises: N3547
Project: JTC1.22.32 Programming Language C++
Reply to: Walter E. Brown <webrown.cpp@gmail.com>
www.numericalsolution.co.uk/random-numbers/ 76ACCU 2014
Manufacturer
Throughpu
t (Mbit/s)
Price Intro Date
Comscire 32 $1,495 2013
ID Quantique SA 16 2,230.00 € 2006
Quant-Lab 12 2,700.00 € 2005
TectroLabs 1.4 $329 2013
Flying Stone Tech 0.6 JPY 4,000 2013
TRNG98 0.5 620.00 € 2009
ubld.it 0.4 $49.95 2014
Araneus 0.4 159 € 2003
0 5 10 15 20 25 30 35
Comscire
ID Quantique SA
Quant-Lab
TectroLabs
Flying Stone Technology
TRNG98
ubld.it
Araneus
Throughput (Mbit/s)
Hardware Random Number Generator
www.numericalsolution.co.uk/random-numbers/ 77ACCU 2014
www.numericalsolution.co.uk/random-numbers/ 78ACCU 2014
cuRAND Engines Normal Distributions(cuRAND Engines)
www.numericalsolution.co.uk/random-numbers/ 79ACCU 2014
“Skip ahead or saving state to avoid overlap is the caller’s responsibility”
seed[n] = seed[n-1] + 1000; (.discard(i * iterations);)
www.numericalsolution.co.uk/random-numbers/ 80ACCU 2014
Do you realise, our brain is a source of random numbers?!
 It accesses the past and present data, generates random scenarios
and simulates future be it next minute or next decade;
 It is a sophisticated random generator, very smooth simulator, but it is bit of biased;
 Pessimistic brain skewed towards negative random;
 Optimistic brain skewed towards positive random;
www.numericalsolution.co.uk/random-numbers/ 81ACCU 2014
Thank You!
www.numericalsolution.co.uk/random-numbers/ 82ACCU 2014

More Related Content

What's hot

Difcalc10week2
Difcalc10week2Difcalc10week2
Difcalc10week2
Carlos Vázquez
 
Pseudo Random Number
Pseudo Random NumberPseudo Random Number
Pseudo Random Number
Hemant Chetwani
 
Antiderivatives And Slope Fields
Antiderivatives And Slope FieldsAntiderivatives And Slope Fields
Antiderivatives And Slope Fieldsseltzermath
 
Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithm
Shilpa Hait
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssionseShikshak
 
Machine learning Introduction
Machine learning IntroductionMachine learning Introduction
Machine learning Introduction
Kuppusamy P
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
Kuppusamy P
 
Clipping
ClippingClipping
Clipping
AMIT VIRAMGAMI
 
Circle & curve clipping algorithm
Circle & curve clipping algorithmCircle & curve clipping algorithm
Circle & curve clipping algorithm
Mohamed El-Serngawy
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear Regression
Andrew Ferlitsch
 
Website optimisation with Multi Armed Bandit algorithms
Website optimisation with Multi Armed Bandit algorithmsWebsite optimisation with Multi Armed Bandit algorithms
Website optimisation with Multi Armed Bandit algorithms
Dennis Hotson
 
10CSL67 CG LAB PROGRAM 2
 10CSL67 CG LAB PROGRAM 2 10CSL67 CG LAB PROGRAM 2
10CSL67 CG LAB PROGRAM 2
Vanishree Arun
 
PC 1 continuity
PC 1 continuityPC 1 continuity
PC 1 continuityvhiggins1
 
clipping
clippingclipping
clipping
HiteshJain007
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
Rohit Shrivastava
 
Artificial Neural Networks
Artificial Neural NetworksArtificial Neural Networks
Artificial Neural Networks
Saif Al-Kalbani
 
Geometric algorithms
Geometric algorithmsGeometric algorithms
Geometric algorithms
Ganesh Solanke
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operatorsdishti7
 
Lect7 viewing in2d
Lect7 viewing in2dLect7 viewing in2d
Lect7 viewing in2d
BCET
 
Differential Calculus - 1st Partial Review
Differential Calculus - 1st Partial ReviewDifferential Calculus - 1st Partial Review
Differential Calculus - 1st Partial Review
Carlos Vázquez
 

What's hot (20)

Difcalc10week2
Difcalc10week2Difcalc10week2
Difcalc10week2
 
Pseudo Random Number
Pseudo Random NumberPseudo Random Number
Pseudo Random Number
 
Antiderivatives And Slope Fields
Antiderivatives And Slope FieldsAntiderivatives And Slope Fields
Antiderivatives And Slope Fields
 
Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithm
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Machine learning Introduction
Machine learning IntroductionMachine learning Introduction
Machine learning Introduction
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
 
Clipping
ClippingClipping
Clipping
 
Circle & curve clipping algorithm
Circle & curve clipping algorithmCircle & curve clipping algorithm
Circle & curve clipping algorithm
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear Regression
 
Website optimisation with Multi Armed Bandit algorithms
Website optimisation with Multi Armed Bandit algorithmsWebsite optimisation with Multi Armed Bandit algorithms
Website optimisation with Multi Armed Bandit algorithms
 
10CSL67 CG LAB PROGRAM 2
 10CSL67 CG LAB PROGRAM 2 10CSL67 CG LAB PROGRAM 2
10CSL67 CG LAB PROGRAM 2
 
PC 1 continuity
PC 1 continuityPC 1 continuity
PC 1 continuity
 
clipping
clippingclipping
clipping
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Artificial Neural Networks
Artificial Neural NetworksArtificial Neural Networks
Artificial Neural Networks
 
Geometric algorithms
Geometric algorithmsGeometric algorithms
Geometric algorithms
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
Lect7 viewing in2d
Lect7 viewing in2dLect7 viewing in2d
Lect7 viewing in2d
 
Differential Calculus - 1st Partial Review
Differential Calculus - 1st Partial ReviewDifferential Calculus - 1st Partial Review
Differential Calculus - 1st Partial Review
 

Viewers also liked

Pseudo Random Number Generators
Pseudo Random Number GeneratorsPseudo Random Number Generators
Pseudo Random Number Generators
Darshini Parikh
 
Nblym ppt
Nblym pptNblym ppt
Nblym pptCsizi96
 
Estimating default risk in fund structures
Estimating default risk in fund structuresEstimating default risk in fund structures
Estimating default risk in fund structures
IFMR
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functionsankita44
 
Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2IIUM
 
Key Bible Concepts Test Questions
Key Bible Concepts Test QuestionsKey Bible Concepts Test Questions
Key Bible Concepts Test Questions
Nilo del Mundo
 
Introduction to Quantum Monte Carlo
Introduction to Quantum Monte CarloIntroduction to Quantum Monte Carlo
Introduction to Quantum Monte Carlo
Claudio Attaccalite
 
PPC purposal(a complete emerging plan)
PPC purposal(a complete emerging plan)PPC purposal(a complete emerging plan)
PPC purposal(a complete emerging plan)
Laxman Bajgain
 
Random
RandomRandom
Random
ivoelenchev
 
What to do when pseudo- is not good enough
What to do when pseudo- is not good enoughWhat to do when pseudo- is not good enough
What to do when pseudo- is not good enough
Angel Marchev
 
Random Numbers Certified By Bells Theorem
Random Numbers Certified By Bells TheoremRandom Numbers Certified By Bells Theorem
Random Numbers Certified By Bells Theorem
David Kemp
 
Management System Key Elements
Management System Key ElementsManagement System Key Elements
Management System Key Elements
Phil Byrne
 
A securing symmetric key distribution
A securing symmetric key distributionA securing symmetric key distribution
A securing symmetric key distribution
vinothp2k
 
Numerals
NumeralsNumerals
Numerals
Inka Vilén
 
IBM System Storage Data Encryption
IBM System Storage Data EncryptionIBM System Storage Data Encryption
IBM System Storage Data Encryption
Behroz Zarrinfar
 
Key Management System Presentation: Jaguar
Key Management System Presentation: JaguarKey Management System Presentation: Jaguar
Key Management System Presentation: Jaguarm_phull
 
Google drive on linux
Google drive on linuxGoogle drive on linux
Google drive on linux
維泰 蔡
 
Algorithms for Computer Games - lecture slides 2009
Algorithms for Computer Games - lecture slides 2009Algorithms for Computer Games - lecture slides 2009
Algorithms for Computer Games - lecture slides 2009
Jouni Smed
 
Random Number Generators
Random Number GeneratorsRandom Number Generators
Random Number Generators
Andrew Collier
 

Viewers also liked (20)

Pseudo Random Number Generators
Pseudo Random Number GeneratorsPseudo Random Number Generators
Pseudo Random Number Generators
 
Nblym ppt
Nblym pptNblym ppt
Nblym ppt
 
Estimating default risk in fund structures
Estimating default risk in fund structuresEstimating default risk in fund structures
Estimating default risk in fund structures
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
 
Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2
 
Key Bible Concepts Test Questions
Key Bible Concepts Test QuestionsKey Bible Concepts Test Questions
Key Bible Concepts Test Questions
 
Introduction to Quantum Monte Carlo
Introduction to Quantum Monte CarloIntroduction to Quantum Monte Carlo
Introduction to Quantum Monte Carlo
 
PPC purposal(a complete emerging plan)
PPC purposal(a complete emerging plan)PPC purposal(a complete emerging plan)
PPC purposal(a complete emerging plan)
 
Random
RandomRandom
Random
 
What to do when pseudo- is not good enough
What to do when pseudo- is not good enoughWhat to do when pseudo- is not good enough
What to do when pseudo- is not good enough
 
Random Numbers Certified By Bells Theorem
Random Numbers Certified By Bells TheoremRandom Numbers Certified By Bells Theorem
Random Numbers Certified By Bells Theorem
 
Management System Key Elements
Management System Key ElementsManagement System Key Elements
Management System Key Elements
 
A securing symmetric key distribution
A securing symmetric key distributionA securing symmetric key distribution
A securing symmetric key distribution
 
Random numbers
Random numbersRandom numbers
Random numbers
 
Numerals
NumeralsNumerals
Numerals
 
IBM System Storage Data Encryption
IBM System Storage Data EncryptionIBM System Storage Data Encryption
IBM System Storage Data Encryption
 
Key Management System Presentation: Jaguar
Key Management System Presentation: JaguarKey Management System Presentation: Jaguar
Key Management System Presentation: Jaguar
 
Google drive on linux
Google drive on linuxGoogle drive on linux
Google drive on linux
 
Algorithms for Computer Games - lecture slides 2009
Algorithms for Computer Games - lecture slides 2009Algorithms for Computer Games - lecture slides 2009
Algorithms for Computer Games - lecture slides 2009
 
Random Number Generators
Random Number GeneratorsRandom Number Generators
Random Number Generators
 

Similar to Random number generation (in C++) – past, present and potential future

Monte carlo simulation
Monte carlo simulationMonte carlo simulation
Monte carlo simulation
Rajesh Piryani
 
Introduction to MATLAB Programming and Numerical Methods for Engineers 1st Ed...
Introduction to MATLAB Programming and Numerical Methods for Engineers 1st Ed...Introduction to MATLAB Programming and Numerical Methods for Engineers 1st Ed...
Introduction to MATLAB Programming and Numerical Methods for Engineers 1st Ed...
AmeryWalters
 
C programming 28 program
C programming 28 programC programming 28 program
C programming 28 program
F Courses
 
Forelasning4
Forelasning4Forelasning4
Forelasning4
Memo Love
 
21cm cosmology with machine learning (Review))
21cm cosmology with machine learning (Review))21cm cosmology with machine learning (Review))
21cm cosmology with machine learning (Review))
Hayato Shimabukuro
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
SanketAde1
 
Video Games for Autonomous Driving
Video Games for Autonomous DrivingVideo Games for Autonomous Driving
Video Games for Autonomous DrivingArtur Filipowicz
 
Virtual Machines
Virtual MachinesVirtual Machines
Virtual Machines
Joa Ebert
 
Best Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesBest Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' Mistakes
Andrey Karpov
 
Introduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdfIntroduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdf
TulasiramKandula1
 
Model-counting Approaches For Nonlinear Numerical Constraints
Model-counting Approaches For Nonlinear Numerical ConstraintsModel-counting Approaches For Nonlinear Numerical Constraints
Model-counting Approaches For Nonlinear Numerical Constraints
Quoc-Sang Phan
 
Data Visualization With R: Introduction
Data Visualization With R: IntroductionData Visualization With R: Introduction
Data Visualization With R: Introduction
Rsquared Academy
 
Trust Measurement Presentation_Part 3
Trust Measurement Presentation_Part 3Trust Measurement Presentation_Part 3
Trust Measurement Presentation_Part 3
Gan Chun Chet
 
UiA Slam (Øystein Øihusom & Ørjan l. Olsen)
UiA Slam (Øystein Øihusom & Ørjan l. Olsen)UiA Slam (Øystein Øihusom & Ørjan l. Olsen)
UiA Slam (Øystein Øihusom & Ørjan l. Olsen)Øystein Øihusom
 
Midterm revision 2022 without answer.pdf
Midterm revision 2022  without answer.pdfMidterm revision 2022  without answer.pdf
Midterm revision 2022 without answer.pdf
AhmedSalah48055
 
Pycon tati gabru
Pycon tati gabruPycon tati gabru
Pycon tati gabru
Tatiana Gabruseva, PhD
 
The Unicorn's Travel to the Microcosm
The Unicorn's Travel to the MicrocosmThe Unicorn's Travel to the Microcosm
The Unicorn's Travel to the Microcosm
Andrey Karpov
 
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Codemotion
 
Cinemática en coordenadas nromales y tangenciales
Cinemática en coordenadas nromales y tangencialesCinemática en coordenadas nromales y tangenciales
Cinemática en coordenadas nromales y tangenciales
JOSEMAURICIONOROAGAL
 

Similar to Random number generation (in C++) – past, present and potential future (20)

FINAL PROJECT
FINAL PROJECTFINAL PROJECT
FINAL PROJECT
 
Monte carlo simulation
Monte carlo simulationMonte carlo simulation
Monte carlo simulation
 
Introduction to MATLAB Programming and Numerical Methods for Engineers 1st Ed...
Introduction to MATLAB Programming and Numerical Methods for Engineers 1st Ed...Introduction to MATLAB Programming and Numerical Methods for Engineers 1st Ed...
Introduction to MATLAB Programming and Numerical Methods for Engineers 1st Ed...
 
C programming 28 program
C programming 28 programC programming 28 program
C programming 28 program
 
Forelasning4
Forelasning4Forelasning4
Forelasning4
 
21cm cosmology with machine learning (Review))
21cm cosmology with machine learning (Review))21cm cosmology with machine learning (Review))
21cm cosmology with machine learning (Review))
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
 
Video Games for Autonomous Driving
Video Games for Autonomous DrivingVideo Games for Autonomous Driving
Video Games for Autonomous Driving
 
Virtual Machines
Virtual MachinesVirtual Machines
Virtual Machines
 
Best Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesBest Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' Mistakes
 
Introduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdfIntroduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdf
 
Model-counting Approaches For Nonlinear Numerical Constraints
Model-counting Approaches For Nonlinear Numerical ConstraintsModel-counting Approaches For Nonlinear Numerical Constraints
Model-counting Approaches For Nonlinear Numerical Constraints
 
Data Visualization With R: Introduction
Data Visualization With R: IntroductionData Visualization With R: Introduction
Data Visualization With R: Introduction
 
Trust Measurement Presentation_Part 3
Trust Measurement Presentation_Part 3Trust Measurement Presentation_Part 3
Trust Measurement Presentation_Part 3
 
UiA Slam (Øystein Øihusom & Ørjan l. Olsen)
UiA Slam (Øystein Øihusom & Ørjan l. Olsen)UiA Slam (Øystein Øihusom & Ørjan l. Olsen)
UiA Slam (Øystein Øihusom & Ørjan l. Olsen)
 
Midterm revision 2022 without answer.pdf
Midterm revision 2022  without answer.pdfMidterm revision 2022  without answer.pdf
Midterm revision 2022 without answer.pdf
 
Pycon tati gabru
Pycon tati gabruPycon tati gabru
Pycon tati gabru
 
The Unicorn's Travel to the Microcosm
The Unicorn's Travel to the MicrocosmThe Unicorn's Travel to the Microcosm
The Unicorn's Travel to the Microcosm
 
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
 
Cinemática en coordenadas nromales y tangenciales
Cinemática en coordenadas nromales y tangencialesCinemática en coordenadas nromales y tangenciales
Cinemática en coordenadas nromales y tangenciales
 

Recently uploaded

Scope Of Macroeconomics introduction and basic theories
Scope Of Macroeconomics introduction and basic theoriesScope Of Macroeconomics introduction and basic theories
Scope Of Macroeconomics introduction and basic theories
nomankalyar153
 
Analyzing the instability of equilibrium in thr harrod domar model
Analyzing the instability of equilibrium in thr harrod domar modelAnalyzing the instability of equilibrium in thr harrod domar model
Analyzing the instability of equilibrium in thr harrod domar model
ManthanBhardwaj4
 
一比一原版(IC毕业证)帝国理工大学毕业证如何办理
一比一原版(IC毕业证)帝国理工大学毕业证如何办理一比一原版(IC毕业证)帝国理工大学毕业证如何办理
一比一原版(IC毕业证)帝国理工大学毕业证如何办理
conose1
 
how to sell pi coins on Bitmart crypto exchange
how to sell pi coins on Bitmart crypto exchangehow to sell pi coins on Bitmart crypto exchange
how to sell pi coins on Bitmart crypto exchange
DOT TECH
 
What website can I sell pi coins securely.
What website can I sell pi coins securely.What website can I sell pi coins securely.
What website can I sell pi coins securely.
DOT TECH
 
what is the best method to sell pi coins in 2024
what is the best method to sell pi coins in 2024what is the best method to sell pi coins in 2024
what is the best method to sell pi coins in 2024
DOT TECH
 
The WhatsPump Pseudonym Problem and the Hilarious Downfall of Artificial Enga...
The WhatsPump Pseudonym Problem and the Hilarious Downfall of Artificial Enga...The WhatsPump Pseudonym Problem and the Hilarious Downfall of Artificial Enga...
The WhatsPump Pseudonym Problem and the Hilarious Downfall of Artificial Enga...
muslimdavidovich670
 
Intro_Economics_ GPresentation Week 4.pptx
Intro_Economics_ GPresentation Week 4.pptxIntro_Economics_ GPresentation Week 4.pptx
Intro_Economics_ GPresentation Week 4.pptx
shetivia
 
Transkredit Finance Company Products Presentation (1).pptx
Transkredit Finance Company Products Presentation (1).pptxTranskredit Finance Company Products Presentation (1).pptx
Transkredit Finance Company Products Presentation (1).pptx
jenomjaneh
 
一比一原版(GWU,GW毕业证)加利福尼亚大学|尔湾分校毕业证如何办理
一比一原版(GWU,GW毕业证)加利福尼亚大学|尔湾分校毕业证如何办理一比一原版(GWU,GW毕业证)加利福尼亚大学|尔湾分校毕业证如何办理
一比一原版(GWU,GW毕业证)加利福尼亚大学|尔湾分校毕业证如何办理
obyzuk
 
Pensions and housing - Pensions PlayPen - 4 June 2024 v3 (1).pdf
Pensions and housing - Pensions PlayPen - 4 June 2024 v3 (1).pdfPensions and housing - Pensions PlayPen - 4 June 2024 v3 (1).pdf
Pensions and housing - Pensions PlayPen - 4 June 2024 v3 (1).pdf
Henry Tapper
 
The secret way to sell pi coins effortlessly.
The secret way to sell pi coins effortlessly.The secret way to sell pi coins effortlessly.
The secret way to sell pi coins effortlessly.
DOT TECH
 
Webinar Exploring DORA for Fintechs - Simont Braun
Webinar Exploring DORA for Fintechs - Simont BraunWebinar Exploring DORA for Fintechs - Simont Braun
Webinar Exploring DORA for Fintechs - Simont Braun
FinTech Belgium
 
USDA Loans in California: A Comprehensive Overview.pptx
USDA Loans in California: A Comprehensive Overview.pptxUSDA Loans in California: A Comprehensive Overview.pptx
USDA Loans in California: A Comprehensive Overview.pptx
marketing367770
 
Donald Trump Presentation and his life.pptx
Donald Trump Presentation and his life.pptxDonald Trump Presentation and his life.pptx
Donald Trump Presentation and his life.pptx
SerdarHudaykuliyew
 
Abhay Bhutada Leads Poonawalla Fincorp To Record Low NPA And Unprecedented Gr...
Abhay Bhutada Leads Poonawalla Fincorp To Record Low NPA And Unprecedented Gr...Abhay Bhutada Leads Poonawalla Fincorp To Record Low NPA And Unprecedented Gr...
Abhay Bhutada Leads Poonawalla Fincorp To Record Low NPA And Unprecedented Gr...
Vighnesh Shashtri
 
how to sell pi coins in all Africa Countries.
how to sell pi coins in all Africa Countries.how to sell pi coins in all Africa Countries.
how to sell pi coins in all Africa Countries.
DOT TECH
 
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Godwin Emmanuel Oyedokun MBA MSc ACA ACIB FCTI FCFIP CFE
 
how to swap pi coins to foreign currency withdrawable.
how to swap pi coins to foreign currency withdrawable.how to swap pi coins to foreign currency withdrawable.
how to swap pi coins to foreign currency withdrawable.
DOT TECH
 
how can I sell/buy bulk pi coins securely
how can I sell/buy bulk pi coins securelyhow can I sell/buy bulk pi coins securely
how can I sell/buy bulk pi coins securely
DOT TECH
 

Recently uploaded (20)

Scope Of Macroeconomics introduction and basic theories
Scope Of Macroeconomics introduction and basic theoriesScope Of Macroeconomics introduction and basic theories
Scope Of Macroeconomics introduction and basic theories
 
Analyzing the instability of equilibrium in thr harrod domar model
Analyzing the instability of equilibrium in thr harrod domar modelAnalyzing the instability of equilibrium in thr harrod domar model
Analyzing the instability of equilibrium in thr harrod domar model
 
一比一原版(IC毕业证)帝国理工大学毕业证如何办理
一比一原版(IC毕业证)帝国理工大学毕业证如何办理一比一原版(IC毕业证)帝国理工大学毕业证如何办理
一比一原版(IC毕业证)帝国理工大学毕业证如何办理
 
how to sell pi coins on Bitmart crypto exchange
how to sell pi coins on Bitmart crypto exchangehow to sell pi coins on Bitmart crypto exchange
how to sell pi coins on Bitmart crypto exchange
 
What website can I sell pi coins securely.
What website can I sell pi coins securely.What website can I sell pi coins securely.
What website can I sell pi coins securely.
 
what is the best method to sell pi coins in 2024
what is the best method to sell pi coins in 2024what is the best method to sell pi coins in 2024
what is the best method to sell pi coins in 2024
 
The WhatsPump Pseudonym Problem and the Hilarious Downfall of Artificial Enga...
The WhatsPump Pseudonym Problem and the Hilarious Downfall of Artificial Enga...The WhatsPump Pseudonym Problem and the Hilarious Downfall of Artificial Enga...
The WhatsPump Pseudonym Problem and the Hilarious Downfall of Artificial Enga...
 
Intro_Economics_ GPresentation Week 4.pptx
Intro_Economics_ GPresentation Week 4.pptxIntro_Economics_ GPresentation Week 4.pptx
Intro_Economics_ GPresentation Week 4.pptx
 
Transkredit Finance Company Products Presentation (1).pptx
Transkredit Finance Company Products Presentation (1).pptxTranskredit Finance Company Products Presentation (1).pptx
Transkredit Finance Company Products Presentation (1).pptx
 
一比一原版(GWU,GW毕业证)加利福尼亚大学|尔湾分校毕业证如何办理
一比一原版(GWU,GW毕业证)加利福尼亚大学|尔湾分校毕业证如何办理一比一原版(GWU,GW毕业证)加利福尼亚大学|尔湾分校毕业证如何办理
一比一原版(GWU,GW毕业证)加利福尼亚大学|尔湾分校毕业证如何办理
 
Pensions and housing - Pensions PlayPen - 4 June 2024 v3 (1).pdf
Pensions and housing - Pensions PlayPen - 4 June 2024 v3 (1).pdfPensions and housing - Pensions PlayPen - 4 June 2024 v3 (1).pdf
Pensions and housing - Pensions PlayPen - 4 June 2024 v3 (1).pdf
 
The secret way to sell pi coins effortlessly.
The secret way to sell pi coins effortlessly.The secret way to sell pi coins effortlessly.
The secret way to sell pi coins effortlessly.
 
Webinar Exploring DORA for Fintechs - Simont Braun
Webinar Exploring DORA for Fintechs - Simont BraunWebinar Exploring DORA for Fintechs - Simont Braun
Webinar Exploring DORA for Fintechs - Simont Braun
 
USDA Loans in California: A Comprehensive Overview.pptx
USDA Loans in California: A Comprehensive Overview.pptxUSDA Loans in California: A Comprehensive Overview.pptx
USDA Loans in California: A Comprehensive Overview.pptx
 
Donald Trump Presentation and his life.pptx
Donald Trump Presentation and his life.pptxDonald Trump Presentation and his life.pptx
Donald Trump Presentation and his life.pptx
 
Abhay Bhutada Leads Poonawalla Fincorp To Record Low NPA And Unprecedented Gr...
Abhay Bhutada Leads Poonawalla Fincorp To Record Low NPA And Unprecedented Gr...Abhay Bhutada Leads Poonawalla Fincorp To Record Low NPA And Unprecedented Gr...
Abhay Bhutada Leads Poonawalla Fincorp To Record Low NPA And Unprecedented Gr...
 
how to sell pi coins in all Africa Countries.
how to sell pi coins in all Africa Countries.how to sell pi coins in all Africa Countries.
how to sell pi coins in all Africa Countries.
 
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
 
how to swap pi coins to foreign currency withdrawable.
how to swap pi coins to foreign currency withdrawable.how to swap pi coins to foreign currency withdrawable.
how to swap pi coins to foreign currency withdrawable.
 
how can I sell/buy bulk pi coins securely
how can I sell/buy bulk pi coins securelyhow can I sell/buy bulk pi coins securely
how can I sell/buy bulk pi coins securely
 

Random number generation (in C++) – past, present and potential future

  • 1. Random number generation (in C++) - past, present and potential future Pattabi Raman pattabi@numericalsolution.co.uk http://www.numericalsolution.co.uk/random-numbers/
  • 2. What is Random? It represents an event or entity, which cannot be determined but only described probabilistically, For example: • Falling rain drops which hit the ground in random pattern, • Distribution of stars with in the universe is a random, and • Babies cry in random and so on www.numericalsolution.co.uk/random-numbers/ 2ACCU 2014
  • 3. What are the use of Random Numbers? They are mainly used in: • Simulations be it a numerical calculation or a cartoon game, • Binning analog data in channel format, • Testing a product and so on. www.numericalsolution.co.uk/random-numbers/ 3ACCU 2014
  • 4. What is the plan of this presentation? • Historic evolution of random numbers and their applications, • Modern development and implementation in C++, and beyond. www.numericalsolution.co.uk/random-numbers/ 4ACCU 2014
  • 5. How did random numbers and their applications evolve? www.numericalsolution.co.uk/random-numbers/ 5ACCU 2014
  • 6. Since pre-historic period, random numbers were generated from dice for gambling www.numericalsolution.co.uk/random-numbers/ 6ACCU 2014
  • 7. In bronze age gambling became unethical Random numbers from dice Made Kings to give up their crowns to opponents www.numericalsolution.co.uk/random-numbers/ 7ACCU 2014
  • 8. In 300 BC, Prof Chanakya of Takshashila University prescribed laws and taxations to regulate gambling. www.numericalsolution.co.uk/random-numbers/ 8ACCU 2014
  • 9. In 1650 Blaise Pascal, who introduced the computer in the form of his mechanical calculator www.numericalsolution.co.uk/random-numbers/ 9ACCU 2014
  • 10. Designed perpetual motion experiment www.numericalsolution.co.uk/random-numbers/ 10ACCU 2014
  • 11. That lead the way to the Roulette www.numericalsolution.co.uk/random-numbers/ 11ACCU 2014
  • 12. Casino de Monte-Carlo Monaco - MC 98000 www.numericalsolution.co.uk/random-numbers/ 12ACCU 2014
  • 13. Casino de Monte-Carlo Monaco - MC 98000 www.numericalsolution.co.uk/random-numbers/ 13ACCU 2014
  • 14. In 1930 FERMIAC The Monte Carlo trolley Random number generator statistical sampling techniques www.numericalsolution.co.uk/random-numbers/ 14ACCU 2014
  • 15. While playing solitaire during his recovery from a surgery, he had thought about playing hundreds of games to estimate statistically the probability of a successful outcome This lead to the idea of Monte Carlo Method! Stan Ulam In 1940 www.numericalsolution.co.uk/random-numbers/ 15ACCU 2014
  • 16. ENIAC - first electronic general-purpose computer Used Monte Carlo Method and employed random numbers to solve complicated problems. Obtained random numbers from:  nuclear radioactivity;  voltage fluctuation;  Solar flare and stored in punch cards. But reading from punch card was very slow! John von Neumann www.numericalsolution.co.uk/random-numbers/ 16ACCU 2014
  • 17. ENIAC - first electronic general-purpose computer Used Monte Carlo Method and employed random numbers to solve complicated problems. So, he developed pseudo-random numbers, using the 13th century mid-square method Let, Seed 𝒙 𝟎 = 0.7891, then 𝒙 𝟎 𝟐 = 0.62 2678 81 ⇒ 𝒙 𝟏 = 0.2678 𝒙 𝟏 𝟐 = 0.07 1716 84 ⇒ 𝒙 𝟐 = 0.1716 𝒙 𝟐 𝟐 = 0.02 9446 56 ⇒ 𝒙 𝟑 = 0.9446 John von Neumann "Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin." www.numericalsolution.co.uk/random-numbers/ 17ACCU 2014
  • 18. John von Neumann He applied Monte Carlo Methods and www.numericalsolution.co.uk/random-numbers/ 18ACCU 2014
  • 19. John von Neumann He applied Monte Carlo Methods and www.numericalsolution.co.uk/random-numbers/ 19ACCU 2014
  • 20. John von Neumann He applied Monte Carlo Methods and www.numericalsolution.co.uk/random-numbers/ 20ACCU 2014
  • 21. John von Neumann He applied Monte Carlo Methods and replicators www.numericalsolution.co.uk/random-numbers/ 21ACCU 2014
  • 22. John von Neumann He applied Monte Carlo Methods and replicators www.numericalsolution.co.uk/random-numbers/ 22ACCU 2014
  • 23. In 1957 Fortran (Formula Translating System) Multiplicative Congruential Generator (MCG) 𝑋 𝑛 = (a𝑋 𝑛−1) mod m RANDOM = MOD(A*SEED,M) PRINT*, RANDOM SEED = RANDOM Linear congruential generator 𝑋 𝑛 = (a𝑋 𝑛−1 + b) mod m www.numericalsolution.co.uk/random-numbers/ 23ACCU 2014
  • 24. static unsigned long int next = 1; int rand(void) // RAND_MAX assumed to be 32767 { static const unsigned long int a = 1103515245; static const unsigned short b = 12345; next = next * a + b; return (unsigned int)(next/65536) % 32768;; } void srand(unsigned int seed) { next = seed; } In 1972 C www.numericalsolution.co.uk/random-numbers/ 24ACCU 2014
  • 25. static unsigned long int next = 1; int rand(void) // RAND_MAX assumed to be 32767 { static const unsigned long int a = 1103515245; static const unsigned short b = 12345; next = next * a + b; return (unsigned int)(next/65536) % 32768; } void srand(unsigned int seed) { next = seed; } In 1972 C www.numericalsolution.co.uk/random-numbers/ 25ACCU 2014
  • 26. #include <cstdlib> #include <cstdio> #include <ctime> int main() { srand(time(NULL)); printf("Random numbers:n"); float random = 0; for (int i = 0; i < 10; ++i) { random = (float) rand()/RAND_MAX; printf("%fn", random); } } Random numbers: 0.003143 0.569353 0.033021 0.545427 0.627430 0.035096 0.817377 0.055635 0.282266 0.187628 www.numericalsolution.co.uk/random-numbers/ 26ACCU 2014
  • 27.  BCPL  1972 C (proximity to hardware) 1967 Simula (OOP)  ALGOL  Fortran  Speedcoding 1983 C++ C with Classes (of Simula) Machine code (100011 00011)  Assembly language (mov ax,@data) www.numericalsolution.co.uk/random-numbers/ 27ACCU 2014
  • 28. int rand (void); void srand (unsigned int seed); #define RAND_MAX 32767 std::random_shuffle (data.begin(), data.end()); Initial data is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Shuffled data is {6, 10, 5, 2, 3, 7, 9, 8, 1, 4} In 1983 C++ www.numericalsolution.co.uk/random-numbers/ 28ACCU 2014
  • 30. Random Numbers - Numberphile http://www.youtube.com/watch?v=SxP30euw3-0 www.numericalsolution.co.uk/random-numbers/ 30ACCU 2014
  • 33. In 2011 C++ 11 <random> (26.5 Random number generation N3797) Random = Distribution(Engine) www.numericalsolution.co.uk/random-numbers/ 33ACCU 2014
  • 34. In 2011 C++ 11 <random> (26.5 Random number generation N3797) Random = Distribution(Engine) Engines: linear_congruential_engine subtract_with_carry_engine mersenne_twister_engine Engines Adaptors: discard_block_engine independent_bits_engine shuffle_order_engine True random number generator: random_device Distributions: Uniform distributions Normal distributions Bernoulli distributions Rate-based distributions Piecewise distributions Canonical numbers www.numericalsolution.co.uk/random-numbers/ 34ACCU 2014
  • 35. linear_congruential_engine 𝑋 𝑛 = (a𝑋 𝑛−1 + b) mod m minstd_rand0: a = 16807; b = 0; m = 2, 147, 483, 647 minstd_rand: a = 48271; b = 0; m = 2, 147, 483, 647 www.numericalsolution.co.uk/random-numbers/ 35ACCU 2014
  • 36. linear_congruential_engine 𝑋 𝑛 = (a𝑋 𝑛−1 + b) mod m minstd_rand0: a = 16807; b = 0; m = 2, 147, 483, 647 minstd_rand: a = 48271; b = 0; m = 2, 147, 483, 647 www.numericalsolution.co.uk/random-numbers/ 36ACCU 2014
  • 37. subtract_with_carry_engine 𝑋n = (𝑋n−S - 𝑋n−R - 𝑐𝑦n−1) mod m where, c𝑦n = (𝑋n−S - 𝑋n−R - 𝑐𝑦n−1 < 0) ? 1 : 0; 𝑋 𝑛 = (a𝑋 𝑛−1 + b) mod m = f (𝑋 𝑛−1)Linear Congruential algorithm => Lagged Fibonacci algorithm => f (𝑋 𝑛−1, 𝑋 𝑛−2) => f (𝑋 𝑛−𝑆, 𝑋 𝑛−𝑅); where, S < R < 0 1991 George Marsaglia and Arif Zaman ranlux24_base: 24-bit number S = 10; R = 24; ranlux48_base: 48-bit number S = 5; R = 12; www.numericalsolution.co.uk/random-numbers/ 37ACCU 2014
  • 38. mersenne_twister_engine Period length = Mersenne prime = 219937−1 MT19937 uses a 32-bit word length MT19937-64 uses a 64-bit word length 1997Makoto Matsumoto and Takuji Nishimura “Twisted Generalized Feedback Shift Register” www.numericalsolution.co.uk/random-numbers/ 38ACCU 2014
  • 39. random_device Generates random numbers from hardware where available CRTIMP2_PURE unsigned int __CLRCALL_PURE_OR_CDECL _Random_device(); unsigned int operator()() { return (_Random_device()); } std::random_device rd; std::default_random_engine e1(rd()); www.numericalsolution.co.uk/random-numbers/ 39ACCU 2014
  • 40. void my_random_generator::check_mt19937() { mt19937 engine; static long long random_number = 0; for(int i =0; i != 9999; ++i) engine(); random_number = engine(); // If the implementation is correct then the 10000th consecutive // invocation of a default-constructed object of type mt19937 // shall produce the value 4,123,659,995, ref. C++11 ISO statement. if(random_number == 4123659995) cout << "n Note:nt The pseudorandom number generatornt " << "Mersenne twister: MT19937 has been testednt and " << "it shows it is implemented properly.n"; else cout << "nWarning:nt " << "The pseudorandom number generatornt " << "Mersenne twister: MT19937 has been " << "testednt and it shows it is NOT " << "implemented properly.n"; } Required behaviour: The 10000th consecutive invocation of a default-constructed object of type mt19937 shall produce the value 4123659995. www.numericalsolution.co.uk/random-numbers/ 40ACCU 2014
  • 41. auto dist = bind(uniform_real_distribution<double>{0.0, 1.0}, default_random_engine{}); Random = dist(); // A Uniform Distribution based on default_random_engine: // Using Bind function: www.numericalsolution.co.uk/random-numbers/ 41ACCU 2014
  • 42. class uniform_dist { public: double operator()() { return uniform(engine); } uniform_dist() :uniform{ 0.0, 1.0 } {} void discard(unsigned long long z) { engine.discard(z); } void discard_distribution(unsigned long long z) { for (auto i = z; i != 0; --i) uniform(engine); } uniform_dist(double low, double high) : uniform(low, high) {} private: default_random_engine engine; uniform_real_distribution <double> uniform; }; www.numericalsolution.co.uk/random-numbers/ 42ACCU 2014
  • 43. // A Normal distribution based on // Mersenne Twister engine: class normal_dist { public: double operator()() { return normal(engine); } normal_dist() :normal {0.0, 1.0} {} void discard(unsigned long long z) { engine.discard(z); } void discard_distribution(unsigned long long z) { for (auto i = z; i != 0; --i) normal(engine); } normal_dist(double mean, double std_dev) : normal( mean, std_dev ) {} private: mt19937 engine; normal_distribution<double> normal; }; www.numericalsolution.co.uk/random-numbers/ 43ACCU 2014
  • 44. 0 0.0005 0.001 0.0015 0.002 0.0025 0.003 0.0035 0.004 0.0045 -8 -6 -4 -2 0 2 4 6 8 Φ(X) X 2 = 1 = 0 2 = 1 = 0 Normal distribution www.numericalsolution.co.uk/random-numbers/ 44ACCU 2014
  • 45. 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 -8 -6 -4 -2 0 2 4 6 8 Σ(X) X CDF = 0 2 = 1 0 0.0005 0.001 0.0015 0.002 0.0025 0.003 0.0035 0.004 0.0045 -8 -6 -4 -2 0 2 4 6 8 (X) X 2 = 1 = 0 2 = 1 = 0 Probability normal distribution Cumulative normal distribution www.numericalsolution.co.uk/random-numbers/ 45ACCU 2014
  • 46. 0 0.2 0.4 0.6 0.8 1 1.2 -8 -6 -4 -2 0 2 4 6 8 Σ(X) X CDF = 0 2 = 1 0 0.0005 0.001 0.0015 0.002 0.0025 0.003 0.0035 0.004 0.0045 -8 -6 -4 -2 0 2 4 6 8 (X) X 2 = 1 = 0 2 = 1 = 0 Probability normal distribution Cumulative normal distribution www.numericalsolution.co.uk/random-numbers/ 46ACCU 2014
  • 47. 0 0.2 0.4 0.6 0.8 1 1.2 -8 -6 -4 -2 0 2 4 6 8 Σ(X) X CDF = 0 2 = 1 0 0.0005 0.001 0.0015 0.002 0.0025 0.003 0.0035 0.004 0.0045 -8 -6 -4 -2 0 2 4 6 8 (X) X 2 = 1 = 0 2 = 1 = 0 Probability normal distribution Cumulative normal distribution ≈ Uniform distribution www.numericalsolution.co.uk/random-numbers/ 47ACCU 2014
  • 48. Pseudo-code algorithm for rational approximation The algorithm below assumes p is the input and x is the output. Coefficients in rational approximations. a(1) <- -3.969683028665376e+01 a(2) <- 2.209460984245205e+02 a(3) <- -2.759285104469687e+02 a(4) <- 1.383577518672690e+02 a(5) <- -3.066479806614716e+01 a(6) <- 2.506628277459239e+00 b(1) <- -5.447609879822406e+01 b(2) <- 1.615858368580409e+02 b(3) <- -1.556989798598866e+02 b(4) <- 6.680131188771972e+01 b(5) <- -1.328068155288572e+01 c(1) <- -7.784894002430293e-03 c(2) <- -3.223964580411365e-01 c(3) <- -2.400758277161838e+00 c(4) <- -2.549732539343734e+00 c(5) <- 4.374664141464968e+00 c(6) <- 2.938163982698783e+00 d(1) <- 7.784695709041462e-03 d(2) <- 3.224671290700398e-01 d(3) <- 2.445134137142996e+00 d(4) <- 3.754408661907416e+00 Define break-points. p_low <- 0.02425 p_high <- 1 - p_low Rational approximation for lower region. if 0 < p < p_low q <- sqrt(-2*log(p)) x <- (((((c(1)*q+c(2))*q+c(3))*q+c(4))*q+c(5))*q+c(6)) / ((((d(1)*q+d(2))*q+d(3))*q+d(4))*q+1) endif Rational approximation for central region. if p_low <= p <= p_high q <- p - 0.5 r <- q*q x <- (((((a(1)*r+a(2))*r+a(3))*r+a(4))*r+a(5))*r+a(6))*q / (((((b(1)*r+b(2))*r+b(3))*r+b(4))*r+b(5))*r+1) endif Rational approximation for upper region. if p_high < p < 1 q <- sqrt(-2*log(1-p)) x <- -(((((c(1)*q+c(2))*q+c(3))*q+c(4))*q+c(5))*q+c(6)) / ((((d(1)*q+d(2))*q+d(3))*q+d(4))*q+1) endif http://home.online.no/~pjacklam/notes/invnorm/index.html www.numericalsolution.co.uk/random-numbers/ 48ACCU 2014
  • 49. Please avoid coding random number generators or distributions! Make use of your compliers and www.numericalsolution.co.uk/random-numbers/ 49ACCU 2014
  • 51. Vt = Vt-dt( 1 + r.dt + sigma.phi(). 𝒅𝒕), for(i = 1; i < T; ++i) { dX = phi()*sqrt_dt; dS = S*(r_dt + sigma*dX); S += dS; data[i] = S; } www.numericalsolution.co.uk/random-numbers/ 51ACCU 2014
  • 52. 0 1000 2000 3000 4000 5000 6000 0 1008 2016 3024 4032 5040 6048 1 Mar 1 May 1 Jul 1 Nov 31 Dec1 Jan Your realistic expectation 1 Sep Houseprice(inhundreds) Active hours (excludes holiday hours) www.numericalsolution.co.uk/random-numbers/ 52ACCU 2014
  • 54. 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7 SpeedupS(N) Number of threads (N) without phi() with phi() Amdahl’s law => 𝑃 = 1−𝑆 𝑁 𝑆 1−𝑁 50 60 70 80 90 100 110 1 2 3 4 5 6 7 ParallelportionP(N)in% Number of threads (N) with phi() without phi() www.numericalsolution.co.uk/random-numbers/ 54ACCU 2014
  • 55. 65 70 75 80 85 90 95 0 1 2 3 4 5 %ofParallelcode <random> engine number uniform_int average uniform_real average normal_distribution average <random> engine numbers: 1. minstd_rand0 2. mt19937 3. mt19937_64 4. knuth_b 5. ranlux24_base (Dead Lock with Normal Distribution) 6. ranlux24 (Dead Lock with all!) <random> engine uniform_int uniform_real normal_distribution average stdev average stdev average stdev 1. Linear congruential: minstd_rand0 88.00 2.67 91.89 2.97 79.28 3.60 2. Mersenne twister: mt19937 91.18 2.34 88.67 3.47 79.60 2.61 3. Mersenne twister: mt19937_64 90.68 2.27 87.73 3.10 76.38 3.04 4. Shuffle order: knuth_b 83.77 4.29 86.75 6.12 70.45 5.71 5. Subtract with carry: ranlux24_base 88.09 2.14 83.62 4.74 Dead Lock! 6. Discard block: ranlux24 Dead Lock! Dead Lock! Dead Lock! www.numericalsolution.co.uk/random-numbers/ 55ACCU 2014
  • 57. Thanks to: Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more www.numericalsolution.co.uk/random-numbers/ 57ACCU 2014 template<class UIntType, size_t w, ...> class mersenne_twister_engine { public: .... .... .... explicit mersenne_twister_engine(result_type value = default_seed); template<class Sseq> explicit mersenne_twister_engine(Sseq& q); void seed(result_type value = default_seed); template<class Sseq> void seed(Sseq& q); // generating functions result_type operator()() const; // if const method then it would be safe for concurrency. void discard(unsigned long long z); };
  • 58. Thanks to: Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more www.numericalsolution.co.uk/random-numbers/ 58ACCU 2014 template<class UIntType, size_t w, ...> class mersenne_twister_engine { public: .... .... .... explicit mersenne_twister_engine(result_type value = default_seed); template<class Sseq> explicit mersenne_twister_engine(Sseq& q); void seed(result_type value = default_seed); template<class Sseq> void seed(Sseq& q); // generating functions result_type operator()() const; // but it is not const method. void discard(unsigned long long z); };
  • 59. Thanks to: Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more www.numericalsolution.co.uk/random-numbers/ 59ACCU 2014 template<class UIntType, size_t w, ...> class mersenne_twister_engine { public: .... .... .... explicit mersenne_twister_engine(result_type value = default_seed); template<class Sseq> explicit mersenne_twister_engine(Sseq& q); void seed(result_type value = default_seed); template<class Sseq> void seed(Sseq& q); // generating functions result_type operator()(); void discard(unsigned long long z); }; // As it is not const method, it is not safe for concurrency!
  • 60. Thanks to: Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more www.numericalsolution.co.uk/random-numbers/ 60ACCU 2014 • Declare random number generator as thread_local, then each thread can have independent copy of the random object. • But, it will provide same set of numbers in all threads, so that is multiplication of same data, therefore, that cannot add to statistics!
  • 61. Thanks to: Hans Boehm, Lawrence Crowl, Mike Giles, Peter Jäckel, Stephan T. Lavavej, Nick Maclaren Alisdair Meredith, Roger Orr, I.M. Sobol‘, Herb Sutter, Jonathan Wakely, Michael Wong and more www.numericalsolution.co.uk/random-numbers/ 61ACCU 2014 • Construct independent engines for each thread and set different range!
  • 62. // A Normal distribution based on // Mersenne Twister engine: class normal_dist { public: double operator()() { return normal(engine); } normal_dist() :normal {0.0, 1.0} {} void discard(unsigned long long z) { engine.discard(z); } void discard_distribution(unsigned long long z) { for (auto i = z; i != 0; --i) normal(engine); } normal_dist(double mean, double std_dev) : normal( mean, std_dev ) {} private: mt19937 engine; normal_distribution<double> normal; }; www.numericalsolution.co.uk/random-numbers/ 62ACCU 2014
  • 63. 3.03 1.99 0.95 3.24 2.20 0.98 0.00 0.50 1.00 1.50 2.00 2.50 3.00 3.50 4.00 NO DISCARD DISCARD IN ENGINE DISCARD IN DISTRIBUTION Speed Up (number of threads = 4) Normal distribution Uniform distribution → 𝐼𝑡 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑖𝑒𝑠 𝑡ℎ𝑒 𝑑𝑎𝑡𝑎 𝑛𝑜𝑡 𝑖𝑛𝑐𝑟𝑒𝑎𝑠𝑖𝑛𝑔 𝑡ℎ𝑒 𝑠𝑡𝑎𝑡𝑖𝑠𝑡𝑖𝑐𝑠 www.numericalsolution.co.uk/random-numbers/ 63ACCU 2014 Complexity of ‘discard(number)’, i.e., performance time should be reduced, so number theoreticians can help!
  • 64. Quasi-Random Numbers www.numericalsolution.co.uk/random-numbers/ 64ACCU 2014 • What are they? • How do they differ from Pseudo-random numbers? • Where do they help?
  • 71. Quasi-Random Numbers Low-discrepancy sequence Pseudo-Random Numbers 𝜖 ∝ 𝑐 𝑑 (𝑙𝑛 𝑁) 𝑑 𝑵 d is dimension, i.e., d numbers of random numbers were estimated per iteration. 𝜖 ∝ 1 𝑵 N is number of iterations. Halton Niederreiter-Xing Sobol Linear congruential Subtract with carry Mersenne twister www.numericalsolution.co.uk/random-numbers/ 71ACCU 2014
  • 72. Quasi-Random Numbers Low-discrepancy sequence Pseudo-Random Numbers 𝜖 ∝ 𝑐 𝑑 (𝑙𝑛 𝑁) 𝑑 𝑵 d is dimension, i.e., d numbers of random numbers were estimated per iteration. 𝜖 ∝ 1 𝑵 N is number of iterations. Halton Niederreiter-Xing Sobol –it uses primitive polynomial based bitwise arithmetic without carry - XOR Linear congruential Subtract with carry Mersenne twister y = f(x, random()) + g(x, random()); → 𝑒𝑞𝑢𝑎𝑡𝑖𝑜𝑛 1 P = f(Q, random()) + g(R, random()) + h(T, random()); → 𝑒𝑞𝑢𝑎𝑡𝑖𝑜𝑛 2 www.numericalsolution.co.uk/random-numbers/ 72ACCU 2014
  • 75. “Most programmers will soon discover that the rand() function is completely inadequate because it can only generate a single stream of random numbers. Most games need multiple discrete streams of random numbers”. www.numericalsolution.co.uk/random-numbers/ 75ACCU 2014
  • 76. Potential candidates for C++17: • Dimensionality to random number generations; • Low-discrepancy sequence (Sobol numbers); int pick_a_number( int from, int thru ) { static std::uniform_int_distribution<> d{}; using parm_t = decltype(d)::param_type; return d( global_urng(), parm_t{from, thru} ); } double pick_a_number( double from, double upto ) { static std::uniform_real_distribution<> d{}; using parm_t = decltype(d)::param_type; return d( global_urng(), parm_t{from, upto} ); } Three <random>-related Proposals, v2 Document #: WG21 N3742 Date: 2013-08-30 Revises: N3547 Project: JTC1.22.32 Programming Language C++ Reply to: Walter E. Brown <webrown.cpp@gmail.com> www.numericalsolution.co.uk/random-numbers/ 76ACCU 2014
  • 77. Manufacturer Throughpu t (Mbit/s) Price Intro Date Comscire 32 $1,495 2013 ID Quantique SA 16 2,230.00 € 2006 Quant-Lab 12 2,700.00 € 2005 TectroLabs 1.4 $329 2013 Flying Stone Tech 0.6 JPY 4,000 2013 TRNG98 0.5 620.00 € 2009 ubld.it 0.4 $49.95 2014 Araneus 0.4 159 € 2003 0 5 10 15 20 25 30 35 Comscire ID Quantique SA Quant-Lab TectroLabs Flying Stone Technology TRNG98 ubld.it Araneus Throughput (Mbit/s) Hardware Random Number Generator www.numericalsolution.co.uk/random-numbers/ 77ACCU 2014
  • 79. cuRAND Engines Normal Distributions(cuRAND Engines) www.numericalsolution.co.uk/random-numbers/ 79ACCU 2014
  • 80. “Skip ahead or saving state to avoid overlap is the caller’s responsibility” seed[n] = seed[n-1] + 1000; (.discard(i * iterations);) www.numericalsolution.co.uk/random-numbers/ 80ACCU 2014
  • 81. Do you realise, our brain is a source of random numbers?!  It accesses the past and present data, generates random scenarios and simulates future be it next minute or next decade;  It is a sophisticated random generator, very smooth simulator, but it is bit of biased;  Pessimistic brain skewed towards negative random;  Optimistic brain skewed towards positive random; www.numericalsolution.co.uk/random-numbers/ 81ACCU 2014