Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random numbers with exponential distribution (with mean)?

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?

like image 837
Sadiksha Gautam Avatar asked Jul 15 '12 11:07

Sadiksha Gautam


People also ask

How do you find the mean of an exponential distribution?

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.

How do you generate a random sample of an exponential distribution in R?

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.

How do you generate random numbers with an exponential distribution in Matlab?

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.


1 Answers

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 / lambda1.

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


Distribution as readable ascii chart

#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 -> 

like image 62
Filip Roséen - refp Avatar answered Sep 18 '22 03:09

Filip Roséen - refp