Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/qat/core_ext/integer.rb

Overview

Integer Class extension

Class Method Summary collapse

Class Method Details

.random(length = 1) ⇒ Integer

Generates a random integer with a given number of digits

Parameters:

  • length (Integer) (defaults to: 1)

    number of digits of random integer. Default is 1.

Returns:

Raises:

  • (ArgumentError)

Since:

  • 1.2.0



7
8
9
10
11
# File 'lib/qat/core_ext/integer.rb', line 7

def self.random(length=1)
  raise(ArgumentError, 'Argument should be an Integer!') unless length.is_a?(Integer)
  elements = (0..9).to_a
  [(1..9).to_a.sample, (length-1).times.map { elements.sample }].flatten.join.to_i
end