Class: FsrsRuby::Alea
- Inherits:
-
Object
- Object
- FsrsRuby::Alea
- Defined in:
- lib/fsrs_ruby/alea.rb
Overview
Alea PRNG class
Instance Attribute Summary collapse
-
#c ⇒ Object
Returns the value of attribute c.
-
#s0 ⇒ Object
Returns the value of attribute s0.
-
#s1 ⇒ Object
Returns the value of attribute s1.
-
#s2 ⇒ Object
Returns the value of attribute s2.
Instance Method Summary collapse
-
#initialize(seed = nil) ⇒ Alea
constructor
A new instance of Alea.
- #next ⇒ Object
- #state ⇒ Object
- #state=(new_state) ⇒ Object
Constructor Details
#initialize(seed = nil) ⇒ Alea
Returns a new instance of Alea.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fsrs_ruby/alea.rb', line 34 def initialize(seed = nil) mash = Mash.new @c = 1 @s0 = mash.call(' ') @s1 = mash.call(' ') @s2 = mash.call(' ') seed = Time.now.to_i if seed.nil? @s0 -= mash.call(seed) @s0 += 1 if @s0 < 0 @s1 -= mash.call(seed) @s1 += 1 if @s1 < 0 @s2 -= mash.call(seed) @s2 += 1 if @s2 < 0 end |
Instance Attribute Details
#c ⇒ Object
Returns the value of attribute c.
32 33 34 |
# File 'lib/fsrs_ruby/alea.rb', line 32 def c @c end |
#s0 ⇒ Object
Returns the value of attribute s0.
32 33 34 |
# File 'lib/fsrs_ruby/alea.rb', line 32 def s0 @s0 end |
#s1 ⇒ Object
Returns the value of attribute s1.
32 33 34 |
# File 'lib/fsrs_ruby/alea.rb', line 32 def s1 @s1 end |
#s2 ⇒ Object
Returns the value of attribute s2.
32 33 34 |
# File 'lib/fsrs_ruby/alea.rb', line 32 def s2 @s2 end |
Instance Method Details
#next ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/fsrs_ruby/alea.rb', line 53 def next t = 2091639 * @s0 + @c * 2.3283064365386963e-10 # 2^-32 @s0 = @s1 @s1 = @s2 @c = t.to_i @s2 = t - @c @s2 end |
#state ⇒ Object
62 63 64 |
# File 'lib/fsrs_ruby/alea.rb', line 62 def state { c: @c, s0: @s0, s1: @s1, s2: @s2 } end |
#state=(new_state) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/fsrs_ruby/alea.rb', line 66 def state=(new_state) @c = new_state[:c] @s0 = new_state[:s0] @s1 = new_state[:s1] @s2 = new_state[:s2] end |