Class: Capricious::LFSR

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

Overview

Linear-feedback shift register class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = nil, seed = nil) ⇒ LFSR

initializes



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

def initialize(size=nil, seed=nil)
  size = size || (0.size * 8)
  case size
  when 64
    @ns = SixtyFourBitShifter
    class << self ; include SixtyFourBitShifter ; private :shift_reg ; end
  when 32
    @ns = ThirtyTwoBitShifter
    class << self ; include ThirtyTwoBitShifter ; private :shift_reg ; end
  when 16
    @ns = SixteenBitShifter
    class << self ; include SixteenBitShifter ; private :shift_reg ; end
  else
    @ns = ThirtyTwoBitShifter
    class << self ; include ThirtyTwoBitShifter ; private :shift_reg ; end
  end
  
  reset
end

Instance Attribute Details

#seedObject (readonly)

Returns the value of attribute seed.



23
24
25
# File 'lib/capricious/lfsr.rb', line 23

def seed
  @seed
end

Class Method Details

.new_with_seed(seed) ⇒ Object



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

def LFSR.new_with_seed(seed)
  LFSR.new(nil, seed)
end

Instance Method Details

#next_fObject



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

def next_f
  next_i
  @reg.quo(@ns::MASK.to_f)
end

#next_iObject



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

def next_i
  shift_reg
  @reg
end

#reset(seed = nil) ⇒ Object



60
61
62
63
# File 'lib/capricious/lfsr.rb', line 60

def reset(seed=nil)
  @seed ||= (seed || Time.now.utc.to_i) & @ns::MASK
  @reg = @seed
end