Class: GLW::Random
- Inherits:
-
Object
- Object
- GLW::Random
- Defined in:
- lib/glw/random.rb
Constant Summary collapse
- MASK64 =
(1 << 64) - 1
- MASK32 =
(1 << 32) - 1
- CONST =
0x2545F4914F6CDD1D
Instance Method Summary collapse
- #float ⇒ Object
-
#initialize(seed) ⇒ Random
constructor
A new instance of Random.
- #integer ⇒ Object
- #sample(collection) ⇒ Object
Constructor Details
#initialize(seed) ⇒ Random
7 8 9 |
# File 'lib/glw/random.rb', line 7 def initialize(seed) @state = seed & MASK64 end |
Instance Method Details
#float ⇒ Object
25 26 27 |
# File 'lib/glw/random.rb', line 25 def float integer.to_f / (1 << 32) end |
#integer ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/glw/random.rb', line 15 def integer x = state x = (x ^ (x >> 12)) & MASK64 x = (x ^ (x << 25)) & MASK64 x = (x ^ (x >> 27)) & MASK64 self.state = x (((x * CONST) & MASK64) >> 32) & MASK32 end |
#sample(collection) ⇒ Object
11 12 13 |
# File 'lib/glw/random.rb', line 11 def sample(collection) collection[integer % collection.length] end |