Class: Hlockey::Utils::Rng
- Inherits:
-
Object
- Object
- Hlockey::Utils::Rng
- Defined in:
- lib/hlockey/utils.rb
Overview
Lehmer random number generator, to avoid incompatabilities in RNG between Ruby implementations. From en.wikipedia.org/wiki/Lehmer_random_number_generator#Sample_C99_code.
Constant Summary collapse
- MOD_VAL =
0x7fffffff
Instance Attribute Summary collapse
- #state ⇒ Integer readonly
Instance Method Summary collapse
-
#initialize(seed) ⇒ Rng
constructor
A new instance of Rng.
- #rand(bound = nil) ⇒ Numeric
Constructor Details
Instance Attribute Details
#state ⇒ Integer (readonly)
13 14 15 |
# File 'lib/hlockey/utils.rb', line 13 def state @state end |
Instance Method Details
#rand(bound = nil) ⇒ Numeric
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hlockey/utils.rb', line 25 def rand(bound = nil) if bound.is_a?(Range) start = bound.begin || 0 return rand(bound.end.nil? ? nil : bound.end - start) + start end @state = @state * 48_271 % MOD_VAL return @state if bound.nil? || bound.zero? @state % bound end |