Gaussian random variable

B

bob

I was wondering if anyone can recommend a way to generate a Gaussian
random variable with standard deviation 1.

Thank you.
 
A

Alf P. Steinbach

* (e-mail address removed):
I was wondering if anyone can recommend a way to generate a Gaussian
random variable with standard deviation 1.

Unfortunately the standard library doesn't help you much in this regard.

Check the net. Also, IIRC, the Smalltalk books had some examples.
 
M

Mark P

I was wondering if anyone can recommend a way to generate a Gaussian
random variable with standard deviation 1.

Thank you.

Nothing C++ about this question. Followups set to comp.programming.

Here's one very general approach that can also be applied to your question:

For a general distribution D(x) consider the integral of that
distribution G(x) = integral_{-infinity}^x D(y) dy. The range of G(x)
is then [0,1]. Pick a value z uniformly at random in [0,1] and consider
the inverse function G^{-1}(z). Then the distribution of values of
G^{-1}(z) obeys the original distribution D.

That may sound complicated but it's really not. You have a standard
normal distribution N(x). Integrate that distribution to get:

f(x) = 1/2 (1 + erf(x/sqrt(2))).

Now pick values uniformly at random in [0,1], which ought to be easy
(approximately) in most languages. Then compute f-inverse of those
values to get values obeying a Gaussian distribution. The hard part
here will be inverting the erf function. You'll probably have to rely
on some sort of library for this (or numerically integrate N(x) yourself
and create a lookup table of values of f(x)).

Mark
 
J

James Kanze

I was wondering if anyone can recommend a way to generate a Gaussian
random variable with standard deviation 1.

If you can use Boost, Boost::random has a normal_distribution
class, which you can use with any of its random generator
classes. (Note that this is also in TR1, and will be part of
the next version of C++, so it's possible that your compiler
already supports it.)

See http://www.boost.org/libs/random/random-distributions.html
for the documentation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,351
Latest member
RoxiePulli

Latest Threads

Top