Class: Capricious::MWC5

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed = nil) ⇒ MWC5

Returns a new instance of MWC5.



29
30
31
# File 'lib/capricious/mwc5.rb', line 29

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

Instance Attribute Details

#seedObject (readonly)

Returns the value of attribute seed.



24
25
26
# File 'lib/capricious/mwc5.rb', line 24

def seed
  @seed
end

#seedsObject (readonly)

Returns the value of attribute seeds.



24
25
26
# File 'lib/capricious/mwc5.rb', line 24

def seeds
  @seeds
end

Class Method Details

.new_with_seed(seed) ⇒ Object



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

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

Instance Method Details

#next_fObject



54
55
56
# File 'lib/capricious/mwc5.rb', line 54

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

#next_iObject



50
51
52
# File 'lib/capricious/mwc5.rb', line 50

def next_i
  shift_ks
end

#reset(seed = nil) ⇒ Object



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

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