I am trying to generate exponentially distributed random number with mean equal to 1. I know how to get random number for normal distribution with mean and standard deviation. We can get it by normal(mean, standard_deviation)
, but I don't know how to get random number for exponential distribution.
Can anyone help me with this?
The mean of the exponential distribution is calculated using the integration by parts. Hence, the mean of the exponential distribution is 1/λ. Thus, the variance of the exponential distribution is 1/λ2.
The code for generating random exponential distribution in R is rexp(n,lamda) where n refers to the sample size and lambda is the rate parameter. The mean of exponential distribution is 1/lambda and the standard deviation is also 1/lambda. In our exercise, lambda is set to 0.2 for all the simulations.
r = exprnd( mu ) generates a random number from the exponential distribution with mean mu . r = exprnd( mu , sz1,...,szN ) generates an array of random numbers from the exponential distribution, where sz1,...,szN indicates the size of each dimension.
With C++11 the standard actually guarantees that there is a RNG following the requirements of exponential-distribution available in the STL, and fittingly the object-type has a very descriptive name.
The mean in an exponentially distributed random generator is calculated by the formula E[X] = 1 / lambda
1.
std::exponential_distribution
has a constructor taking lambda as an argument, so we can easily create an object following your rules by calculating the value of lambda and passing this to our generator.
std::exponential_distribution rng (1/1); // lambda = 1 / E[X]
Footnotes
1. according to en.wikipedia.org - Exponential distribution > Mean, variance, moments and median
#include <iomanip>
#include <random>
#include <map>
#include <iostream>
int
main (int argc, char *argv[])
{
double const exp_dist_mean = 1;
double const exp_dist_lambda = 1 / exp_dist_mean;
std::random_device rd;
std::exponential_distribution<> rng (exp_dist_lambda);
std::mt19937 rnd_gen (rd ());
/* ... */
std::map<int, int> result_set;
for (int i =0; i < 100000; ++i)
++result_set[rng (rnd_gen) * 4];
for (auto& v : result_set) {
std::cout << std::setprecision (2) << std::fixed;
std::cout << v.first/4.f << " - " << (v.first+1)/4.f << " -> ";
std::cout << std::string (v.second/400, '.') << std::endl;
if (v.second/400 == 0)
break;
}
}
0.00 - 0.25 -> ........................................................
0.25 - 0.50 -> ...........................................
0.50 - 0.75 -> .................................
0.75 - 1.00 -> .........................
1.00 - 1.25 -> ....................
1.25 - 1.50 -> ...............
1.50 - 1.75 -> ............
1.75 - 2.00 -> .........
2.00 - 2.25 -> .......
2.25 - 2.50 -> .....
2.50 - 2.75 -> ....
2.75 - 3.00 -> ...
3.00 - 3.25 -> ..
3.25 - 3.50 -> ..
3.50 - 3.75 -> .
3.75 - 4.00 -> .
4.00 - 4.25 -> .
4.25 - 4.50 ->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With