Class: LFSR::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max = nil, seed = nil) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
# File 'lib/lfsr.rb', line 9

def initialize(max = nil, seed = nil)
  # The C code depends on this being the first ivar to work properly.
  @reg = 1

  # This should be the second variable.
  @max = (max || self.class.const_get(:MASK) - 1) + 1

  reset seed
end

Instance Attribute Details

#maxObject

Returns the value of attribute max.



6
7
8
# File 'lib/lfsr.rb', line 6

def max
  @max
end

#regObject (readonly) Also known as: peek

Returns the value of attribute reg.



5
6
7
# File 'lib/lfsr.rb', line 5

def reg
  @reg
end

#seedObject (readonly)

Returns the value of attribute seed.



7
8
9
# File 'lib/lfsr.rb', line 7

def seed
  @seed
end

Instance Method Details

#reset(new_seed) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
# File 'lib/lfsr.rb', line 19

def reset(new_seed)
  @reg = @seed = (new_seed || Time.now.utc.to_i) & self.class.const_get(:MASK)
  raise ArgumentError, "The seed #{new_seed} is unacceptable for this register size" if new_seed && @reg == 0
  @reg = @seed = (new_seed || Time.now.utc.to_i) & self.class.const_get(:MASK) while @reg == 0
end