[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppBoostRandom

(C++) (Boost C++ library) random

Boost C++ library for calculating random numbers.

How to: get a random number from a normal distribution?

  1. include <boost/random/normal_distribution.hpp>
  2. include <boost/random/lagged_fibonacci.hpp>
  3. include <iostream>
  4. include <vector>
int main() { const double mean = 10.0; const double sigma = 1.0; boost::normal_distribution<double> norm_dist(mean, sigma); boost::lagged_fibonacci19937 engine; //Make a histogram const int size = static_cast<int>(mean) * 2; std::vector<int> histo(size,0); for (int i=0; i<1000000; ++i) { const double value = norm_dist.operator () <boost::lagged_fibonacci19937>((engine)); int index = value; index = (index < 0 ? 0 : index); index = (index > size - 1 ? size - 1 : index); ++histo[index]; } //Output histogram for (int i=0; i<size; ++i) { std::cout << histo[i] << std::endl; } return 0; }


Code links



last edited (November 11, 2006) by bilderbikkel, Number of views: 2490, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.