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.



1871
1872
1873
# File 'opal/opal/corelib/array.rb', line 1871

def initialize(rng)
  @rng = rng
end

Instance Method Details

#rand(size) ⇒ Object



1875
1876
1877
1878
1879
1880
1881
# File 'opal/opal/corelib/array.rb', line 1875

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