Class: Capricious::MWC5
- Inherits:
-
Object
- Object
- Capricious::MWC5
- Defined in:
- lib/capricious/mwc5.rb
Overview
Multiply-with-carry pseudorandom number generator. Algorithm due to George Marsaglia.
Instance Attribute Summary collapse
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
-
#seeds ⇒ Object
readonly
Returns the value of attribute seeds.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(seed = nil) ⇒ MWC5
constructor
A new instance of MWC5.
- #next_f ⇒ Object
- #next_i ⇒ Object
- #reset(seed = nil) ⇒ Object
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
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
26 27 28 |
# File 'lib/capricious/mwc5.rb', line 26 def seed @seed end |
#seeds ⇒ Object (readonly)
Returns the value of attribute seeds.
26 27 28 |
# File 'lib/capricious/mwc5.rb', line 26 def seeds @seeds end |
Class Method Details
Instance Method Details
#next_f ⇒ Object
56 57 58 |
# File 'lib/capricious/mwc5.rb', line 56 def next_f next_i.quo(0xffffffff.to_f) end |
#next_i ⇒ Object
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 |