You are here:

C++/exponentially distributed random number

Advertisement


Question
I am doing simulation in C++ and I need random number generator using exponential distribution. Could you help me?

Answer
Use std::exponential_distribution<> in conjunction with a random number generator such as the mersenne twister.

For example, to generate and print out twenty random double values distributed exponentially with a lambda of 2.0:

#include <random>
#include <ctime>
#include <iostream>

int main()
{
       std::mt19937 rng( std::time(0) ) ;
       std::exponential_distribution<double> distrib(2.0) ;
       for( int i = 0 ; i < 10 ; ++i ) std::cout << std::fixed << distrib(rng) << '\n' ;
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.