Module: Random::Implementation
- Defined in:
- lib/vendor/backports-3.3.5/lib/backports/extra/random/implementation.rb
Overview
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.
Instance Attribute Summary collapse
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #bytes(nb) ⇒ Object
- #initialize(seed = 0) ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(ary) ⇒ Object
- #rand(limit = Backports::Undefined) ⇒ Object
- #srand(new_seed = 0) ⇒ Object
Instance Attribute Details
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
8 9 10 |
# File 'lib/vendor/backports-3.3.5/lib/backports/extra/random/implementation.rb', line 8 def seed @seed end |
Instance Method Details
#==(other) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/vendor/backports-3.3.5/lib/backports/extra/random/implementation.rb', line 42 def ==(other) other.is_a?(Random) && seed == other.seed && left == other.send(:left) && state == other.send(:state) end |
#bytes(nb) ⇒ Object
36 37 38 39 40 |
# File 'lib/vendor/backports-3.3.5/lib/backports/extra/random/implementation.rb', line 36 def bytes(nb) nb = Backports.coerce_to_int(nb) raise ArgumentError, "negative size" if nb < 0 @mt.random_bytes(nb) end |
#initialize(seed = 0) ⇒ Object
10 11 12 13 |
# File 'lib/vendor/backports-3.3.5/lib/backports/extra/random/implementation.rb', line 10 def initialize(seed = 0) super() srand(seed) end |
#marshal_dump ⇒ Object
49 50 51 |
# File 'lib/vendor/backports-3.3.5/lib/backports/extra/random/implementation.rb', line 49 def marshal_dump @mt.marshal_dump << @seed end |
#marshal_load(ary) ⇒ Object
53 54 55 56 57 |
# File 'lib/vendor/backports-3.3.5/lib/backports/extra/random/implementation.rb', line 53 def marshal_load(ary) @seed = ary.pop @mt = MT19937.allocate @mt.marshal_load(ary) end |
#rand(limit = Backports::Undefined) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/vendor/backports-3.3.5/lib/backports/extra/random/implementation.rb', line 22 def rand(limit = Backports::Undefined) case limit when Backports::Undefined @mt.random_float when Float limit * @mt.random_float unless limit <= 0 when Range _rand_range(limit) else limit = Backports.coerce_to_int(limit) @mt.random_integer(limit) unless limit <= 0 end || raise(ArgumentError, "invalid argument #{limit}") end |