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?

on 2009-01-22 11:41

on 2009-01-22 12:15

On Jan 22, 11:38 am, Bart Braem <bart.br...@gmail.com> wrote: > For simulation work, I want to use multiple, independent random number > generators. Does anyone know up to date implementations? 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=17...

on 2009-01-22 12:41

On Jan 22, 12:13 pm, Lars Christensen <lar...@belunktum.dk> wrote: > > http://www.splatbang.com/rubyquiz/quiz.rhtml?id=17... 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!

on 2009-01-22 13:00

Bart Braem wrote: > On Jan 22, 12:13ï¿½pm, Lars Christensen <lar...@belunktum.dk> wrote: >> >> http://www.splatbang.com/rubyquiz/quiz.rhtml?id=17... > > 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

on 2009-01-22 14:23

On Jan 22, 2009, at 5:39 AM, Bart Braem wrote: >> 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=17... > > 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

on 2009-01-23 15:10

On Jan 22, 2:20 pm, James Gray <ja...@grayproductions.net> wrote: > > > 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 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!

on 2009-01-23 15:36

On Jan 23, 3:07 pm, Bart Braem <bart.br...@gmail.com> wrote: > > >>> number > > > I indeed mean separate deterministic PRNGs with fixed seeds for > 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).

on 2009-01-23 15:40

On Jan 23, 3:07 pm, Bart Braem <bart.br...@gmail.com> wrote: > > >>> number > > > I indeed mean separate deterministic PRNGs with fixed seeds for > 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[i]}" 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.

on 2009-01-23 16:00

On Jan 23, 3:34 pm, Lars Christensen <lar...@belunktum.dk> wrote: > > > > >>http://www.splatbang.com/rubyquiz/quiz.rhtml?id=17... > > 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). 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?

on 2009-01-23 16:30

```
> rb-gsl gives problems with installation
Out of interest, what kind of problems ?
Chris
```

on 2009-01-23 16:35

```
Le 23 janvier 2009 à 15:59, Bart Braem a écrit :
> 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
```

on 2009-01-23 16:56

On Jan 23, 3:59 pm, Bart Braem <bart.br...@gmail.com> wrote: > > > > > On Jan 22, 12:13 pm, Lars Christensen <lar...@belunktum.dk> wrote: > > > > >> reproducible sequences? Then take a look at this Ruby Quiz: > > > > James Edward Gray II > > 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...

on 2009-01-23 16:56

On Jan 23, 4:28 pm, Chris Lowis <chris.lo...@gmail.com> wrote: > > rb-gsl gives problems with installation > > 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.

on 2009-01-23 16:57

On Jan 23, 4:33 pm, "F. Senault" <f...@lacave.net> wrote: > > uses the standard kernel rand. > 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 2009-01-23 20:00

On Jan 23, 2009, at 9:53 AM, Bart Braem wrote: >>> about 5 times slower, unfortunately. I am calling the random number >> 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. How random do the numbers have to be? If a simple LCG is sufficient, that's trivial to implement in Ruby. Need something more? Check out this thread: <http://groups.google.com/group/sci.crypt/browse_th... > Scroll down to the post by George Marsaglia, where he discusses a bit about Mersenne and others, and provides simple C code for an alternative, simpler than Mersenne, and would probably be very easy to translate to Ruby and so have multiple generators.

on 2009-01-23 21:05

Le 23 janvier 2009 à 16:55, Bart Braem a écrit : > On Jan 23, 4:33 pm, "F. Senault" <f...@lacave.net> wrote: > 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[i]) 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

on 2009-01-25 19:38

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/912db0052151d4018... 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 -- eki@vying.org

on 2009-01-26 21:11

Bart Braem wrote: > 1.upto(10) do |i| > puts "hits for #{i}: #{res[i]}" > 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[i]}" end The extension is at: http://redshift.sourceforge.net/isaac-0.1/

on 2009-01-27 11:41

On Jan 25, 7:34 pm, Eric K Idema <e...@vying.org> wrote: > seed: > > > 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? > > That random gem looks perfect, it works fine and fast. Thanks for the suggestion!