Class: Capricious::MWC5

Inherits:
Object
  • Object
show all
Defined in:
lib/capricious/mwc5.rb

Overview

Multiply-with-carry pseudorandom number generator. Algorithm due to George Marsaglia.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed = nil) ⇒ MWC5

Returns a new instance of MWC5.



31
32
33
# File 'lib/capricious/mwc5.rb', line 31

def initialize(seed=nil)
  reset(seed)
end

Instance Attribute Details

#seedObject (readonly)

Returns the value of attribute seed.



26
27
28
# File 'lib/capricious/mwc5.rb', line 26

def seed
  @seed
end

#seedsObject (readonly)

Returns the value of attribute seeds.



26
27
28
# File 'lib/capricious/mwc5.rb', line 26

def seeds
  @seeds
end

Class Method Details

.new_with_seed(seed) ⇒ Object



27
28
29
# File 'lib/capricious/mwc5.rb', line 27

def MWC5.new_with_seed(seed)
  MWC5.new(seed)
end

Instance Method Details

#next_fObject



56
57
58
# File 'lib/capricious/mwc5.rb', line 56

def next_f
  next_i.quo(0xffffffff.to_f)
end

#next_iObject



52
53
54
# File 'lib/capricious/mwc5.rb', line 52

def next_i
  shift_ks
end

#reset(seed = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/capricious/mwc5.rb', line 35

def reset(seed=nil)
  @seed ||= (seed || Time.now.utc.to_i)
  unless @seeds
    seeder = LFSR.new_with_seed(@seed)
    @seeds = [seeder.next_i & 0xffffffff]
    4.times do
      9.times do
        # the observed lag-10 autocorrelation of the LFSRs is low
        seeder.next_i
      end
      @seeds << (seeder.next_i & 0xffffffff)
    end
  end
  
  @x,@y,@z,@w,@v = @seeds
end