Reputation: 1348
I am trying to generate a series of random numbers that conform to the exponential distribution. Fortunately, C++ provides such template class exponential_distribution
, which can generate floating-point values according to an exponential distribution, providing the reverse of the mean value (i.e., the arrival rate).
On the other side, each generated value represents the interval between two random events that are independent but statistically defined by a constant average arrival rate, i.e., its lambda, λ. So lambda is the reverse of the mean value.
After generating such random values, I want to check if the generated randoms are accurate, so I compute the arrival rate according to the generated randoms by lambda' = the number of generated randoms (number of arrivals)/the sum of the generated randoms (time duration)
, which is the arrival rate.
However the result turns out that lambda
and lambda'
are very different. The following is my code, where lambda=20.0
.
#include<iostream>
#include<random>
#include<fstream>
#include<map>
using namespace std;
int main(){
mt19937 mt; //The Mersenne Twister is a pseudorandom number generator (PRNG), default_seed is a member constant, defined as 5489u;
exponential_distribution<double> exponential_intervals(20.0);
double interval;
double timestamp = 0;
for(int j = 0; j < 20; j ++){
interval = exponential_intervals(mt);
cout<<interval<<endl;
timestamp += interval; //generate exponentional distribution randoms, in micro seconds
}
return 0;
}
The generated randoms are:
0.118112
0.0900931
0.00679023
0.173476
0.122309
0.0124894
0.0500325
0.0184205
0.00513157
0.0396175
0.016321
0.0104363
0.0395801
0.247252
0.157921
0.2822
0.167461
0.171627
The sum is 1.82
, so lambda'=20/1.82=10.98
, which is so different from lambda
.
Can some tell me how to improve the accuracy of the generated randoms?
Upvotes: 0
Views: 282
Reputation: 254621
The standard deviation of the distribution is the same as the mean, mu = 1/lambda
. Remembering the Central Limit Theorem, the deviation of sample means, for sample size N
, will be approximately mu/sqrt(N)
. For a sample size of 20, that is quite large - about 22% of the value you're measuring - so you're likely to get a rather inaccurate estimate.
Increasing the sample size, perhaps to 10000 to give a deviation of around 1%, gives a better estimate: http://ideone.com/9xjQwg
Upvotes: 2