Class: RandomOrg::Rng

Inherits:
Object
  • Object
show all
Defined in:
lib/random_org/rng.rb

Overview

Use random.org as a Random Number Generator (RNG). Suitable for use in things like:

[1,3,5,78,9,5,3].sample(random: RandomOrg::Rng.new)
# => 5

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bytes(size = nil) ⇒ String

Returns a random binary string containing size bytes.

Returns:

  • (String)

    a random binary string containing size bytes



18
19
20
# File 'lib/random_org/rng.rb', line 18

def self.bytes(size = nil)
  RandomOrg.random_bytes(size)
end

.rand(max = nil) ⇒ Numeric

When max is an Integer, rand returns a random integer greater than or equal to zero and less than max.

When max is a Float, rand returns a random floating point number between 0.0 and max, including 0.0 and excluding max.

When max is a Range, rand returns a random number where range.member?(number) == true.

Parameters:

  • max (nil, Integer, Float, Range) (defaults to: nil)

    maximum

Returns:

  • (Numeric)

    a random number in the interval 0 <= n < max

Raises:



48
49
50
51
52
53
54
55
# File 'lib/random_org/rng.rb', line 48

def self.rand(max = nil)
  return max.to_a.sample(random: RandomOrg::Rng.new) if max.is_a? Range
  return RandomOrg.random_number if max.nil? || max.zero?
  return RandomOrg.random_number(max) if max.is_a? Integer
  return build_float_rand(max) if max.is_a? Float

  raise ArgumentError, 'Given argument must be correct type.'
end

Instance Method Details

#bytes(size = nil) ⇒ String

Returns a random binary string containing size bytes.

Returns:

  • (String)

    a random binary string containing size bytes



12
13
14
# File 'lib/random_org/rng.rb', line 12

def bytes(size = nil)
  RandomOrg::Rng.random_bytes(size)
end

#rand(max = nil) ⇒ Numeric

When max is an Integer, rand returns a random integer greater than or equal to zero and less than max.

When max is a Float, rand returns a random floating point number between 0.0 and max, including 0.0 and excluding max.

When max is a Range, rand returns a random number where range.member?(number) == true.

Parameters:

  • max (Numeric) (defaults to: nil)

    maximum

Returns:

  • (Numeric)

    a random number in the interval 0 <= n < max



33
34
35
# File 'lib/random_org/rng.rb', line 33

def rand(max = nil)
  RandomOrg::Rng.rand(max)
end