Advertisement

Exponential random numbers

Started by September 25, 2002 07:01 AM
1 comment, last by thelamberto 22 years, 4 months ago
Hi, I couldn't use search so posted, hope there isn't a good answer to this already. I have an exponential random number generator in my sensor simulation. The problem is that this takes excessive time to compute and I need a 10 times faster method. I admit that to start with I used the brute force method with drand48 and log functions to take a uniform random number [0,1] and exponentially distribute it using the cumulative distribution. Now I need to optimise drastically. I just wondered if anyone had an ideas on whether precomputed look-up tables or random numbers, my own log algorithm or random number generator would be a good way to go? Cheers. [edited by - thelamberto on September 25, 2002 8:02:10 AM] [edited by - thelamberto on September 25, 2002 8:20:48 AM]
I''ve heard people praise the Mersenne Twister

http://www-personal.engin.umich.edu/~wagnerr/MersenneTwister.html

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement
boost::random offers several random number generators AND several distribution adapters.


    #include <boost/random.hpp>// Generate a seed in the 'usual' waysrand( time( NULL ) );unsigned long seed = rand();// I chose a Mersenne Twister pseudo-RNG object // but you could have picked any one of a number// of available PRNGs.boost::mt19937 rng( seed ); // generate a random number in the 'usual' range.unsigned long foo = rng(); // uniformly distributed// Create an exponential distribution adapter object,// which will use the random number generator we just// created, and will have a distribution parameter // lambdadouble lambda = 123.4; // for exampleboost::exponential_distribution dist( rng, lambda );// get a random number with the new distributiondouble bar = dist();  // exponentially distributed    


It's a simple as that.
You can arrange them the way you like it best (faster RNG or whatnot).

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on September 25, 2002 4:09:11 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement