Generating Normally Distributed Random Numbers?

M

Michael Craig

[Note: parts of this message were removed to make it a legal post.]

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
 
M

M. Edward (Ed) Borasky

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". :)
--
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
 
S

steve

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.
 
P

Phlip

steve said:
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...)
 
H

hadley wickham

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
 
M

M. Edward (Ed) Borasky

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.
--
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
 
M

Michael Craig

[Note: parts of this message were removed to make it a legal post.]

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
 
A

Axel Etzold

Dear Michael,

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
 
J

Joel VanderWerf

Michael said:
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
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top