Separate random number generators?

B

Bart Braem

For simulation work, I want to use multiple, independent random number
generators. Does anyone know up to date implementations?

I found randomr but that is inactive sinds 2001 and clearly marked
alpha (http://rubyvm.sourceforge.net/subprojects/randomr/)
I also found some projects from MoonWolf from 2004 but his site
(www.moonwolf.com, see e.g. http://raa.ruby-lang.org/project/random-mt19937/)
is offline. Which makes it hard to get to the code.
There is a generator that uses certain sites, but that is not really
an option.
I am now going to look at the isaac gem, but its version number 0.0.2
does not make me feel very happy.

Did anyone find better implemenations of PRNGs?
 
B

Bart Braem

What do you mean by independent RNG? Numbers drawn from a single RNG
are usually independent enough.

Do you mean separate deterministic PRNGs with fixed seeds for
reproducible sequences? Then take a look at this Ruby Quiz:

http://www.splatbang.com/rubyquiz/quiz.rhtml?id=173_Not_So_Random

I indeed mean separate deterministic PRNGs with fixed seeds for
reproducible sequences. Interesting to see this was a Ruby Quiz! Are
there solutions without the use of DRb and other libraries?

Thanks for your fast reply!
 
C

Chris Lowis

Bart said:
I indeed mean separate deterministic PRNGs with fixed seeds for
reproducible sequences. Interesting to see this was a Ruby Quiz! Are
there solutions without the use of DRb and other libraries?

Thanks for your fast reply!

I know this doesn't meet your criteria of independence from other
libraries, but the GSL (GNU Scientific Library) has a number (of
different algorithms for seeded random number generation. It's fairly
easy to call these functions from Ruby using the rb-gsl
(http://rb-gsl.rubyforge.org/) bindings. Here's some examples:
http://rb-gsl.rubyforge.org/rng.html

I've also written a bit about that in the past on my blog
(http://blog.chrislowis.co.uk).

Hope this helps,

Chris
 
J

James Gray

I indeed mean separate deterministic PRNGs with fixed seeds for
reproducible sequences. Interesting to see this was a Ruby Quiz! Are
there solutions without the use of DRb and other libraries?

Sure. See the first solution link on that page (also shown in the
quiz write-up).

James Edward Gray II
 
B

Bart Braem

Sure.  See the first solution link on that page (also shown in the  
quiz write-up).

James Edward Gray II

The first solution is of course not that efficient, I will be
generating lots of random numbers. I'm going to take a look at ruby-
gsl, the ISAAC library gives strange results...

Thanks for your help!
 
L

Lars Christensen

The first solution is of course not that efficient, I will be
generating lots of random numbers. I'm going to take a look at ruby-
gsl, the ISAAC library gives strange results...

Thanks for your help!

I do think that my own solution (last on the page) is efficient, but i
can't vouch for the statistical properties (it reseeds the Mersenne
Twister in Ruby every time a number is drawn, although randomly).
 
B

Bart Braem

The first solution is of course not that efficient, I will be
generating lots of random numbers. I'm going to take a look at ruby-
gsl, the ISAAC library gives strange results...

Thanks for your help!

Do not use ISAAC, running this simple test:

rng = Crypt::ISAAC.new
res = Hash.new()
1.upto(100000) do |i|
random = rng.rand
larger = (random * 10).to_i
res[larger] ||= 0
res[larger] += 1
end
1.upto(10) do |i|
puts "hits for #{i}: #{res}"
end

Shows a large bias towards numbers below 0.4, unless I have an error
in this simple script of course.
rb-gsl gives problems with installation, I am going to try the Drb
solution.
 
B

Bart Braem

I do think that my own solution (last on the page) is efficient, but i
can't vouch for the statistical properties (it reseeds the Mersenne
Twister in Ruby every time a number is drawn, although randomly).

I've tested this setup, with just one RNG. Running my unit tests is
about 5 times slower, unfortunately. I am calling the random number
very often, more than 10.000 times in 10 seconds in a simulation that
uses the standard kernel rand.
I can't afford this slowdown, but it seems as though there are no
other solutions that do not use a pure-ruby library?
 
C

Chris Lowis

rb-gsl gives problems with installation

Out of interest, what kind of problems ?

Chris
 
F

F. Senault

Le 23 janvier 2009 à 15:59, Bart Braem a écrit :
I've tested this setup, with just one RNG. Running my unit tests is
about 5 times slower, unfortunately. I am calling the random number
very often, more than 10.000 times in 10 seconds in a simulation that
uses the standard kernel rand.
I can't afford this slowdown, but it seems as though there are no
other solutions that do not use a pure-ruby library?

Can you estimate the number of random numbers you'll need ? Maybe you
could pre-generate sequences of numbers in a few arrays, then patch the
rand method to actually read the array instead of generating the
number ?

The start time and memory consumption will be a lot higher, but maybe it
makes a good compromise ?

Fred
 
B

Bart Braem

I've tested this setup, with just one RNG. Running my unit tests is
about 5 times slower, unfortunately. I am calling the random number
very often, more than 10.000 times in 10 seconds in a simulation that
uses the standard kernel rand.
I can't afford this slowdown, but it seems as though there are no
other solutions that do not use a pure-ruby library?

To follow up: using the Slave library results in quite unstable
behaviour on OSX, sometimes the slave processes can not be found.
Chmodding /tmp does change a bit, but it still causes an overloaded
system in long tests.

I can't share code as it is part of ongoing research, but doing
something seemingly easy like integrating separate RNGs is causing
headaches...
 
B

Bart Braem

Out of interest, what kind of problems ?

The fact that I can't quickly install new software on the grid I use
to run the simulations. If these were pure ruby implementations it
would not be hard to use gems to install something temporarily,
installing GSL is harder.
 
B

Bart Braem

Le 23 janvier 2009 à 15:59, Bart Braem a écrit :



Can you estimate the number of random numbers you'll need ?  Maybe you
could pre-generate sequences of numbers in a few arrays, then patch the
rand method to actually read the array instead of generating the
number ?

The start time and memory consumption will be a lot higher, but maybe it
makes a good compromise ?

That is an idea, but quick calculations show the need of up to 100.000
random numbers. Which is quite a lot to calculate and most importantly
store and retrieve again.
 
M

Matthew Moss

That is an idea, but quick calculations show the need of up to 100.000
random numbers. Which is quite a lot to calculate and most importantly
store and retrieve again.

How random do the numbers have to be? If a simple LCG is sufficient, =20
that's trivial to implement in Ruby.

Need something more? Check out this thread:
=
<http://groups.google.com/group/sci.crypt/browse_thread/thread/305c507efbe=
85be4=20
Scroll down to the post by George Marsaglia, where he discusses a bit =20=

about Mersenne and others, and provides simple C code for an =20
alternative, simpler than Mersenne, and would probably be very easy to =20=

translate to Ruby and so have multiple generators.
 
F

F. Senault

Le 23 janvier 2009 à 16:55, Bart Braem a écrit :
That is an idea, but quick calculations show the need of up to 100.000
random numbers. Which is quite a lot to calculate and most importantly
store and retrieve again.

On a box of mine (a 2.6GHz dual core AMD 64 CPU), it takes 10 seconds to
make 10 arrays of 1.000.000 random numbers, and memory consumption was
about 500M. As for the access times :

ruby 1.9.1p0 (2009-01-20 revision 21700) [amd64-freebsd7]
Seeding in 8.921912
user system total real
Pseudo seqs 93.429688 0.218750 93.648438 ( 93.614218)
Real rand 108.500000 0.000000 108.500000 (108.456800)

The first is accessing the 10 arrays of numbers to pick 1.000.000
numbers between 1 and 26, the second uses the old rand method with no
reseeding whatsoever.

On the other hand, I don't know the exact kind of use you have, but...
you could also generate sequences of a few thousands in arrays and
simply cycle through them (i.e. reuse the numbers a few times). If
you're concerned about random distribution, this wouldn't be a problem ?

(Uglyish) code :

#! /usr/local/bin/ruby

require "benchmark"

t = Time.new

NUM_SEQ = 10
SEEDS = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
NUM_NUMS = 1_000_000

$numbers = []

NUM_SEQ.times do |i|
srand(SEEDS)
a = Array.new(NUM_NUMS) { rand }
$numbers << a
end

$curr_seq = 0
$curr_index = 0
$curr_nums = $numbers[0]
$indexes = Array.new(NUM_SEQ, 0)

puts "Seeding in #{"%2.6f" % (Time.new - t)}"

module Kernel
alias old_rand rand
def swap_seq(seq)
$indexes[$curr_seq] = $curr_index
$curr_seq = seq
$curr_index = $indexes[seq]
$curr_nums = $numbers[seq]
end
def reset_seq(seq)
$indexes[seq] = 0
$curr_index = 0 if seq == $curr_seq
end
def rand(x = nil)
r = $curr_nums[$curr_index]
$curr_index += 1
r = (r * x).to_i if x
r
end
end

acc = 1_000_000
n = 100

Benchmark.bm 20 do |x|
x.report "Pseudo seqs" do
n.times do
ns = 0
s = ""
nq = 0
acc.times do |z|
s = (rand(26) + 65).chr
nq += 1
if nq == 1000
ns = (ns + 1) % NUM_SEQ
swap_seq(ns)
nq = 0
end
end
NUM_SEQ.times { |q| reset_seq(q) }
end
end
x.report "Real rand" do
n.times do
s = ""
acc.times do |z|
s = (old_rand(26) + 65).chr
end
GC.start
end
end
end

Fred
 
E

Eric K Idema

Are you aware of the random gem? I think it's exactly what you're looking for:

http://random.rubyforge.org/rdoc/index.html

I use it in my game library to provide repeatable random numbers (with the
requirement that the library backs a server, so I need to have one rng per
game).

If you need a pure ruby solution, I made a limited port of the random gem
as well. It lacks some of the api, but draws the same numbers for a given
seed:

http://github.com/eki/vying/blob/912db0052151d40185309b62ceee83733b34bfd5/lib/vying/random.rb

It's part of my game library, but it's only one file so you can just extract
the rng if you want to use it.

Eric
 
J

Joel VanderWerf

Bart said:
Do not use ISAAC, running this simple test:

rng = Crypt::ISAAC.new
res = Hash.new()
1.upto(100000) do |i|
random = rng.rand
larger = (random * 10).to_i
res[larger] ||= 0
res[larger] += 1
end
1.upto(10) do |i|
puts "hits for #{i}: #{res}"
end

Shows a large bias towards numbers below 0.4, unless I have an error
in this simple script of course.
rb-gsl gives problems with installation, I am going to try the Drb
solution.


I use my own wrapper around Bob Jenkins' ISAAC library. Based on his
comments on ruby-talk a few years ago, I chose to use the 256 rather
than 16 entry state vectors (it turns out not to affect performance).
It's served well for simulation purposes.

Your code, modified slightly, gives the following results:

hits for 0: 10213
hits for 1: 9830
hits for 2: 9902
hits for 3: 9928
hits for 4: 9951
hits for 5: 10111
hits for 6: 10012
hits for 7: 9940
hits for 8: 9896
hits for 9: 10217

Here's the example:

require 'isaac'

rng = ISAAC.new
#rng.srand([...]) seed with up to 256 entries (optional)
res = Hash.new()
1.upto(100000) do |i| # 100000
random = rng.rand
larger = (random * 10).to_i
res[larger] ||= 0
res[larger] += 1
end
0.upto(9) do |i|
puts "hits for #{i}: #{res}"
end


The extension is at:

http://redshift.sourceforge.net/isaac-0.1/
 
B

Bart Braem

Are you aware of the random gem?  I think it's exactly what you're looking for:

 http://random.rubyforge.org/rdoc/index.html

I use it in my game library to provide repeatable random numbers (with the
requirement that the library backs a server, so I need to have one rng per
game).

If you need a pure ruby solution, I made a limited port of the random gem
as well.  It lacks some of the api, but draws the same numbers for a given
seed:

 http://github.com/eki/vying/blob/912db0052151d40185309b62ceee83733b34....

It's part of my game library, but it's only one file so you can just extract
the rng if you want to use it.

Eric

That random gem looks perfect, it works fine and fast.

Thanks for the suggestion!
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top