Class: Array::SampleRandom

Inherits:
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.


1785
1786
1787
# File 'opal/opal/corelib/array.rb', line 1785

def initialize(rng)
  @rng = rng
end

Instance Method Details

#rand(size) ⇒ Object

Raises:


1789
1790
1791
1792
1793
1794
1795
# File 'opal/opal/corelib/array.rb', line 1789

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

  random
end