Ming
Ming

Reputation: 215

How do I randomly sample a normal distribution with a given mean and standard error in C++?

How to get a random number for given mean and standard error in C++? Are there any built-in functions that can be used straightway?

I am aware of some examples, which using mean and standard deviation to get a random number. However, I do not know the number of replications and only have the mean and standard error, so I would not be able to calculate the standard deviation. Any thoughts?

I appreciate your time and help. Thanks. :-)

Upvotes: 0

Views: 109

Answers (2)

Alex Moreno
Alex Moreno

Reputation: 152

A general answer would be to get a uniform random number on a unit [0,1] and then apply a transformation to map that value to the distribution you need. For a normal distribution, check this post out. Converting a Uniform Distribution to a Normal Distribution

Upvotes: 0

alcedine
alcedine

Reputation: 949

There's std::normal_distribution in <random> in C++11, as well as other distributions. You'll need to separately create a generator object and a distribution object; the cppreference.com page I linked has a good example of usage.

Prior to C++11, nothing like that exists in the standard library, though you might want to use Boost.

Upvotes: 1

Related Questions