Module | Random::Implementation |
In: |
lib/backports/1.9.2/random/implementation.rb
|
Implementation corresponding to the actual Random class of Ruby The actual random generator (mersenne twister) is in MT19937. Ruby specific conversions are handled in bits_and_bytes. The high level stuff (argument checking) is done here.
seed | [R] |
# File lib/backports/1.9.2/random/implementation.rb, line 10 10: def initialize(seed = 0) 11: super() 12: srand(seed) 13: end
# File lib/backports/1.9.2/random/implementation.rb, line 42 42: def ==(other) 43: other.is_a?(Random) && 44: seed == other.seed && 45: left == other.send(:left) && 46: state == other.send(:state) 47: end
# File lib/backports/1.9.2/random/implementation.rb, line 36 36: def bytes(nb) 37: nb = Backports.coerce_to_int(nb) 38: raise ArgumentError, "negative size" if nb < 0 39: @mt.random_bytes(nb) 40: end
# File lib/backports/1.9.2/random/implementation.rb, line 49 49: def marshal_dump 50: @mt.marshal_dump << @seed 51: end
# File lib/backports/1.9.2/random/implementation.rb, line 53 53: def marshal_load(ary) 54: @seed = ary.pop 55: @mt = MT19937.allocate 56: @mt.marshal_load(ary) 57: end
# File lib/backports/1.9.2/random/implementation.rb, line 22 22: def rand(limit = Backports::Undefined) 23: case limit 24: when Backports::Undefined 25: @mt.random_float 26: when Float 27: limit * @mt.random_float unless limit <= 0 28: when Range 29: _rand_range(limit) 30: else 31: limit = Backports.coerce_to_int(limit) 32: @mt.random_integer(limit) unless limit <= 0 33: end || raise(ArgumentError, "invalid argument #{limit}") 34: end