Class: Array.self::SampleRandom

Inherits:
Object
  • Object
show all
Defined in:
opal/opal/corelib/array.rb

Instance Method Summary collapse

Constructor Details

#initialize(rng) ⇒ SampleRandom

Returns a new instance of SampleRandom.



1822
1823
1824
# File 'opal/opal/corelib/array.rb', line 1822

def initialize(rng)
  @rng = rng
end

Instance Method Details

#rand(size) ⇒ Object



1826
1827
1828
1829
1830
1831
1832
# File 'opal/opal/corelib/array.rb', line 1826

def rand(size)
  random = `$coerce_to(#{@rng.rand(size)}, #{::Integer}, 'to_int')`
  ::Kernel.raise ::RangeError, 'random value must be >= 0' if `random < 0`
  ::Kernel.raise ::RangeError, 'random value must be less than Array size' unless `random < size`

  random
end