Generating Normally Distributed Random Numbers?

Does anybody know of a good method in Ruby to generate normally
distributed
random numbers? The application I have in mind at the moment is trivial,
but
I can think of a number of statistics-oriented applications.

Thanks, all,
Mike

On Sun, 2008-08-17 at 11:24 +0900, Michael Craig wrote:

Does anybody know of a good method in Ruby to generate normally distributed
random numbers? The application I have in mind at the moment is trivial, but
I can think of a number of statistics-oriented applications.

Thanks, all,
Mike

The algorithm for generating Gaussian random numbers is
language-independent. It’s pretty simple to code if you have a good
generator of uniform random numbers from 0 to 1. It involves generating
pairs of uniform random numbers and doing a little math that involves
rectangular and polar coordinates.

Given that, you can now search Google with the query “Gaussian random
numbers”. :slight_smile:

M. Edward (Ed) Borasky
ruby-perspectives.blogspot.com

“A mathematician is a machine for turning coffee into theorems.” –
Alfréd Rényi via Paul Erdős

If I remember my basic stats correctly from the dim distant past, isn’t
the mean of a series of N random numbers normally distributed?

Regards

Steve.

steve wrote:

If I remember my basic stats correctly from the dim distant past, isn’t
the mean of a series of N random numbers normally distributed?

There’s “normal” and then there’s “normal”. (-:

(One is your casual meaning - evenly distributed numbers - and the other
is
numbers that occupy the geometric “normal” of some function…)

On Sun, 2008-08-17 at 13:19 +0900, hadley wickham wrote:

A quick and dirty way of generating a normally distributed random
number is to take the average of 6 uniformly distributed numbers
(because of the central limit theorem). It’s by no means perfect, but
it’s an old and useful trick.

Only if you’re pressed for CPU time, like in an embedded environment.
IIRC it’s 12, not 6. But the “standard” way with transcendental
functions is much more accurate.

Hadley


M. Edward (Ed) Borasky
ruby-perspectives.blogspot.com

“A mathematician is a machine for turning coffee into theorems.” –
Alfréd Rényi via Paul Erdős

On Sat, Aug 16, 2008 at 11:01 PM, Phlip [email protected] wrote:

steve wrote:

If I remember my basic stats correctly from the dim distant past, isn’t
the mean of a series of N random numbers normally distributed?

There’s “normal” and then there’s “normal”. (-:

(One is your casual meaning - evenly distributed numbers - and the other is
numbers that occupy the geometric “normal” of some function…)

A quick and dirty way of generating a normally distributed random
number is to take the average of 6 uniformly distributed numbers
(because of the central limit theorem). It’s by no means perfect, but
it’s an old and useful trick.

Hadley

Thanks, all. I tried some generic Googling and found an implementation
of
whats called the Box-Muller transformation. I’ve pretty much got it
figured
out now, and I’d gladly post what I come up with when I’m done. Thanks
to
Eric for what you posted, very helpful.

The “trivial application” I have in mind is for writing some simple code
to
cut a deck of cards realistically. I’m working on some groundwork that
will
allow for ruby-based cards simulation. I’ve already written a working
program for simulating a simple solitaire game called mod10, with the
end
goal of calculating how often a computer, making “perfect” decisions,
wins
the game.

Mike

On Sat, Aug 16, 2008 at 10:24 PM, Michael Craig
<[email protected]

Dear Michael,

Does anybody know of a good method in Ruby to generate normally
distributed
random numbers? The application I have in mind at the moment is trivial,
but
I can think of a number of statistics-oriented applications.

have a look at the statistics2 package in the RAA (Ruby Application
Archive).
It does many statistical calculations, such as random number generation,
in Ruby.
The distributions involved in statistical tests are there, also.

The “trivial application” I have in mind is for writing some simple code
to
cut a deck of cards realistically. I’m working on some groundwork that
will
allow for ruby-based cards simulation. I’ve already written a working
program for simulating a simple solitaire game called mod10, with the end
goal of calculating how often a computer, making “perfect” decisions, wins
the game.

Should you come to a point where statistics2 doesn’t do what you need, a
further option
is to install the R statistical language and its Ruby bindings rsruby.

Best regards,

Axel

Michael Craig wrote:

Does anybody know of a good method in Ruby to generate normally distributed
random numbers? The application I have in mind at the moment is trivial, but
I can think of a number of statistics-oriented applications.

Sounds like you got plenty of answers already, but here’s one more (also
includes log normal and exponential):

http://redshift.sourceforge.net/sci/

Example:

require ‘sci/random’

seq = Random::Normal.new(

:generator => MyGenerator

above is optional, if you want it to work with a generator

other than ruby’s built in one. MyGenerator must support

MyGenerator.new(seed) and #next()

:mean => 10,
:stdev => 2,
:seed => 8732548
)

p seq.next
p seq.next
p seq.next

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs