Uniform Random Number Generator

I need a random number generator that generates random numbers with a
uniform distribution.

I am not sure what rand does in ruby and I can’t seem to find it online
either. My initial assumption is that it follows a uniform distribution,
but
I can’t tell.

Any ideas?

On 5/22/07, Roland M. [email protected] wrote:

I need a random number generator that generates random numbers with a
uniform distribution.

I am not sure what rand does in ruby and I can’t seem to find it online
either. My initial assumption is that it follows a uniform distribution, but
I can’t tell.

Any ideas?

According to the source code for random.c, Ruby’s implementation of
rand uses the Mersenne Twister MT19937 which according to:
http://en.wikipedia.org/wiki/Mersenne_twister has “a very high order
of dimensional equidistribution.”

HTH


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Roland M. wrote:

I need a random number generator that generates random numbers with a
uniform distribution.

I am not sure what rand does in ruby and I can’t seem to find it online
either. My initial assumption is that it follows a uniform distribution,
but
I can’t tell.

Any ideas?

Well, I would think that by definition it has a uniform distribution.
Can you give an example of a RNG that does not have a uniform
distribution?

Dan Z. wrote:

Well, I would think that by definition it has a uniform distribution.
Can you give an example of a RNG that does not have a uniform distribution?

An RNG defined such that its output is the average of the outputs of N
independent uniform RNG’s will, if my very, very shaky memory of first
year undergrad maths can be relied upon, have a Gaussian distribution.
That’s the simplest I can think of…

Dan Z. wrote:

Well, I would think that by definition it has a uniform distribution.
Can you give an example of a RNG that does not have a uniform
distribution?

The stat software I use has RNGs for the Bernoulli, beta, binomial,
Cauchy, chi-square, Erlang, exponential, F, gamma, geometric,
hypergeometric, lognormal, negative binomial, normal, poisson, T,
tabled, triangular, and Weibull distributions, as well as uniform.

Roland M. wrote:

I need a random number generator that generates random numbers with a
uniform distribution.

I am not sure what rand does in ruby and I can’t seem to find it online
either. My initial assumption is that it follows a uniform distribution,
but
I can’t tell.

Any ideas?

Yes, rand() is uniform in the interval 0 <= x < 1. It’s based on
MT19937.

If you have a working ri installation, then you can read about it this
way:

$ ri Kernel#rand | cat
------------------------------------------------------------ Kernel#rand
rand(max=0) => number

  Converts max to an integer using max1 = max.to_i.abs. If the
  result is zero, returns a pseudorandom floating point number
  greater than or equal to 0.0 and less than 1.0. Otherwise, returns
  a pseudorandom integer greater than or equal to zero and less than
  max1. Kernel::srand may be used to ensure repeatable sequences of
  random numbers between different runs of the program. Ruby
  currently uses a modified Mersenne Twister with a period of
  219937-1.

     srand 1234                 #=> 0
     [ rand,  rand ]            #=> [0.191519450163469,

0.49766366626136]
[ rand(10), rand(1000) ] #=> [6, 817]
srand 1234 #=> 1234
[ rand, rand ] #=> [0.191519450163469,
0.49766366626136]

On 5/22/07, Dan Z. [email protected] wrote:

Well, I would think that by definition it has a uniform distribution.
Can you give an example of a RNG that does not have a uniform distribution?

http://xkcd.com/c221.html

Just Kidding.

-Harold

You can use uniform random numbers generated from the GNU Scientific
library, via ruby-gsl,

http://rb-gsl.rubyforge.org/,

see : http://rb-gsl.rubyforge.org/rng.html,

if you should want random numbers that follow some other distribution,
you can always pick a random number in the interval [0,1] and then
numerically find the position at which the cumulative distribution
reaches this value. Do this many times … this gives you a distribution
of points according to the distribution you’re looking for.
You don’t have to program it all by yourself, but you can use
the statistics2 package by Shin-ichiro Hara: see the readme here:

http://blade.nagaokaut.ac.jp/~sinara/ruby/math/statistics2/statistics2-0.50/README

Best regards,

Axel

Tim H. wrote:

Dan Z. wrote:

Well, I would think that by definition it has a uniform distribution.
Can you give an example of a RNG that does not have a uniform
distribution?

The stat software I use has RNGs for the Bernoulli, beta, binomial,
Cauchy, chi-square, Erlang, exponential, F, gamma, geometric,
hypergeometric, lognormal, negative binomial, normal, poisson, T,
tabled, triangular, and Weibull distributions, as well as uniform.

I guess that was a pretty dumb question. But I’m glad I asked it, as I
have never pondered RNGs that aren’t uniform. I’ll probably learn about
the algorithms that generate these other distributions if I ever have
time.

You guys are great. Thanks a lot.

On 5/23/07, Dan Z. [email protected] wrote:

I guess that was a pretty dumb question. But I’m glad I asked it, as I
have never pondered RNGs that aren’t uniform. I’ll probably learn about
the algorithms that generate these other distributions if I ever have time.

Most of these are based on a uniform RNG. They either apply a function
to one or more numbers generated by a uniform random number generator.
Or they use a series of uniform RNs to do a mini-simulation and in the
process generate the required result.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

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