class Faker::Number

def between(from: 1.00, to: 5000.00)

Returns:
  • (Numeric) -

Parameters:
  • to (Numeric) -- The highest number to include.
  • from (Numeric) -- The lowest number to include.
def between(from: 1.00, to: 5000.00)
  Faker::Base.rand_in_range(from, to)
end

def binary(digits: 4)

Returns:
  • (String) -

Parameters:
  • digits (Integer) -- Number of digits to generate the binary as string
def binary(digits: 4)
  bin = ''
  digits.times { bin += rand(2).to_s(2) }
  bin
end

def decimal(l_digits: 5, r_digits: 2)

Returns:
  • (Float) -

Parameters:
  • r_digits (Integer) -- Number of digits that the generated decimal should have to the right of the decimal point.
  • l_digits (Integer) -- Number of digits that the generated decimal should have to the left of the decimal point.
def decimal(l_digits: 5, r_digits: 2)
  l_d = number(digits: l_digits)
  # Ensure the last digit is not zero
  # so it does not get truncated on converting to float
  r_d = generate(r_digits - 1).join + non_zero_digit.to_s
  "#{l_d}.#{r_d}".to_f
end

def decimal_part(digits: 10)

Returns:
  • (String) -

Parameters:
  • digits (Integer) -- Number of digits that the generated number should have.
def decimal_part(digits: 10)
  num = ''
  if digits > 1
    num = non_zero_digit
    digits -= 1
  end
  leading_zero_number(digits: digits) + num.to_s
end

def digit

Returns:
  • (Integer) -
def digit
  rand(10)
end

def generate(count)

def generate(count)
  return [] if count.zero?
  Array.new(count) { digit }
end

def greater_than_zero(number)

def greater_than_zero(number)
  should_be(number, :>)
end

def hexadecimal(digits: 6)

Returns:
  • (String) -

Parameters:
  • digits (Integer) -- Number of digits in the he
def hexadecimal(digits: 6)
  hex = ''
  digits.times { hex += rand(15).to_s(16) }
  hex
end

def leading_zero_number(digits: 10)

Returns:
  • (String) -

Parameters:
  • digits (Integer) -- Number of digits that the generated number should have.
def leading_zero_number(digits: 10)
  "0#{(2..digits).collect { digit }.join}"
end

def less_than_zero(number)

def less_than_zero(number)
  should_be(number, :<)
end

def negative(from: -5000.00, to: -1.00)

Returns:
  • (Float) -

Parameters:
  • to (Integer) -- The higher boundary.
  • from (Integer) -- The lower boundary.
def negative(from: -5000.00, to: -1.00)
  random_number = between(from: from, to: to)
  less_than_zero(random_number)
end

def non_zero_digit

Returns:
  • (Integer) -
def non_zero_digit
  rand(1..9)
end

def normal(mean: 1, standard_deviation: 1)

Returns:
  • (Float) -

Parameters:
  • standard_deviation (Numeric) --
  • mean (Integer) --
def normal(mean: 1, standard_deviation: 1)
  theta = 2 * Math::PI * rand
  rho = Math.sqrt(-2 * Math.log(1 - rand))
  scale = standard_deviation * rho
  mean + scale * Math.cos(theta)
end

def number(digits: 10)

Returns:
  • (Integer) -

Parameters:
  • digits (Integer) -- Number of digits that the generated number should have.
def number(digits: 10)
  return if digits < 1
  return rand(0..9).round if digits == 1
  # Ensure the first digit is not zero
  ([non_zero_digit] + generate(digits - 1)).join.to_i
end

def positive(from: 1.00, to: 5000.00)

Returns:
  • (Float) -

Parameters:
  • to (Integer) -- The higher boundary.
  • from (Integer) -- The lower boundary.
def positive(from: 1.00, to: 5000.00)
  random_number = between(from: from, to: to)
  greater_than_zero(random_number)
end

def should_be(number, method_to_compare)

def should_be(number, method_to_compare)
  if number.send(method_to_compare, 0)
    number
  else
    number * -1
  end
end

def within(range: 1.00..5000.00)

Returns:
  • (Numeric) -

Parameters:
  • range (Range) -- The range from which to generate a number.
def within(range: 1.00..5000.00)
  between(from: range.min, to: range.max)
end