0

I have finished installing Boost, and I am (finally) using it in my program. What I wanted was to be able to generate

  1. Randomly distributed numbers between 0 and 1
  2. Normally distrubted numbers with standard deviation 1

I have accomplished this by the following header file:

#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>

boost::mt19937 rng;

boost::normal_distribution<double> nd(0.0, 1.0);
boost::variate_generator< boost::mt19937, boost::normal_distribution<double> > normal(rng, nd);

boost::uniform_real<float> ur(0.0, 1.0);
boost::variate_generator< boost::mt19937, boost::uniform_real<float> > uniform(rng, ur);

I have two questions:

  1. Is my approach correct? So far it seems consistent in that I get the desired behavíor and it seems like other people do it this way too
  2. I need to simulate a very large sample, and consequently I need a very large number of random numbers, much greater than 10^9. Is there a more efficient way to achieve this than my approach of simply calling uniform() and normal()?
BillyJean
  • 1,537
  • 1
  • 22
  • 39
  • OK; I have refraished my introduction. Please note that I am using the engine "rng" for both distributions. Is this allowed, or do I need to use one distinct engine (of the same type) for each distribution? – BillyJean Feb 05 '13 at 16:10
  • 1
    There is no need to use different engines. Before trying to optimize something you should answer "Do I need to optimize it?". Generate 10^9 numbers and see how much time it does take. – André Puel Feb 05 '13 at 16:21
  • That is a good point. Besides computational time, I was also thinking in terms of seeding the engine before drawing a number of better statistics. Would that make a difference? – BillyJean Feb 05 '13 at 16:26

0 Answers0