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.



1926
1927
1928
# File 'opal/opal/corelib/array.rb', line 1926

def initialize(rng)
  @rng = rng
end

Instance Method Details

#rand(size) ⇒ Object



1930
1931
1932
1933
1934
1935
1936
# File 'opal/opal/corelib/array.rb', line 1930

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