Random Number Generation

Hey guys,
I have to create a program that, at first, generates a real number based on normal probability with given mean and standard deviation. I have found such ways to do this in Matlab and Mathematica software but I need to create my own program because the program will need to do more than just this. I am stuck at this topic. All I have found in forums is this code:

Normal Z;
for(i=0;i<100;i++)
cout << Z.Next << "\n";

This is supposed to make random integers based on normal probability but I need real numbers not integers.

First of all...
What libraries do I need?
Is "Normal" even a c++ function?
Any thoughts would be greatly appreciated!
I do believe that real numbers are integers. Although definitions of integers and natural numbers vary from many different places.

What libraries do I need?


#include <cstdlib>

look into rand() and srand().

EDIT: wikipedia says so:

The integers (from the Latin integer, literally "untouched", hence "whole": the word entire comes from the same origin, but via French[1]) are formed by the natural numbers including 0 (0, 1, 2, 3, ...) together with the negatives of the non-zero natural numbers (−1, −2, −3, ...). Viewed as a subset of the real numbers


Last edited on
boost.random has a normal distribution object just what you are looking for:

http://www.boost.org/doc/libs/1_46_1/doc/html/boost/normal_distribution.html
@king214
you are correct, integers are real numbers but real numbers also include all numbers opposite imaginary/complex numbers. The numbers I need output will have to be decimal numbers and then i will have the program round the numbers. The <cstdlib> is the library I thought I would need. I have looked into rand() and srand() but they do not deal with normal probability. Thank you for your help.

@jsmith
still looking at the website you provided but not sure If I understand it completely yet. Can I input my own values for mu and sigma where the 0 and 1 are?
Yes, it says that the parameters to the constructor are the mean and variance, respectively.
It is probably worth noting that the boost::normal_distribution is not a generator, it is just a transform. You have to connect it to a generator to make it work, you can either do this manually or with a boost::variate_generator.

If you want to do the tranform yourself, a few methods are described here - http://en.wikipedia.org/wiki/Box–Muller_transform
You can also find Mersenne-Twister library. My University Professor used it on some scientific programme.

http://en.wikipedia.org/wiki/Mersenne_twister
Last edited on
Topic archived. No new replies allowed.