Distributed sequential random number generation in Ruby 1.9.2

A class Randomin Ruby 1.9.2 is guaranteed to generate random numbers in the same order, given a specific seed and range. For instance:

r = Random.new(23)
r.rand(100)         # 83
r.rand(100)         # 40

But suppose I want to generate the next number in a sequence on another computer (without re-generating earlier numbers in the sequence). This should be possible, given the previous result. Is there any way to do this with the class Random? Or do I need to write my own implementation of Mersenne twister ?

[ Edit: As indicated in the comments below, it is actually impossible to determine the state of an instance Randomfrom output only, since only part of the state (in particular, low 32 bits) is used for output.]

+5
source share
1 answer

Cannot verify, but the generator can be tuned, according to Marc-André Lafortune here . So this might work:

r = Random.new(23)
r.rand(100)         # 83
r.rand(100)         # 40

File.open("/path/to/file","w") do |f|
  Marshal.dump(r,f)
end

# later, may be on another computer

File.open("/path/to/file","r") do |f|
  @v = Marshal.load(f)
end

puts @v.rand(100)
+2
source

All Articles