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.



1827
1828
1829
# File 'opal/opal/corelib/array.rb', line 1827

def initialize(rng)
  @rng = rng
end

Instance Method Details

#rand(size) ⇒ Object



1831
1832
1833
1834
1835
1836
1837
# File 'opal/opal/corelib/array.rb', line 1831

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