Class: Capricious::LFSR
- Inherits:
-
Object
- Object
- Capricious::LFSR
- Defined in:
- lib/capricious/lfsr.rb
Overview
Linear-feedback shift register class
Instance Attribute Summary collapse
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(size = nil, seed = nil) ⇒ LFSR
constructor
initializes.
- #next_f ⇒ Object
- #next_i ⇒ Object
- #reset(seed = nil) ⇒ Object
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
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
23 24 25 |
# File 'lib/capricious/lfsr.rb', line 23 def seed @seed end |
Class Method Details
Instance Method Details
#next_f ⇒ Object
55 56 57 58 |
# File 'lib/capricious/lfsr.rb', line 55 def next_f next_i @reg.quo(@ns::MASK.to_f) end |
#next_i ⇒ Object
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 |