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



1658
1659
1660
# File 'opal/opal/corelib/array.rb', line 1658

def initialize(rng)
  @rng = rng
end

Instance Method Details

#rand(size) ⇒ Object

Raises:



1662
1663
1664
1665
1666
1667
1668
# File 'opal/opal/corelib/array.rb', line 1662

def rand(size)
  random = Opal.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