Class: Faker::Number

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/number.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.between(from: 1.00, to: 5000.00) ⇒ Numeric

Produces a number between two provided values. Boundaries are inclusive.

Examples:

Faker::Number.between(from: 1, to: 10) #=> 7
Faker::Number.between(from: 0.0, to: 1.0) #=> 0.7844640543957383

Parameters:

  • from (Numeric) (defaults to: 1.00)

    The lowest number to include.

  • to (Numeric) (defaults to: 5000.00)

    The highest number to include.

Returns:

  • (Numeric)


166
167
168
# File 'lib/faker/default/number.rb', line 166

def between(from: 1.00, to: 5000.00)
  Faker::Base.rand_in_range(from, to)
end

.binary(digits: 4) ⇒ String

Produces a number in binary format.

Examples:

Faker::Number.binary(digits: 4) #=> "1001"

Parameters:

  • digits (Integer) (defaults to: 4)

    Number of digits to generate the binary as string

Returns:



130
131
132
133
134
# File 'lib/faker/default/number.rb', line 130

def binary(digits: 4)
  bin = ''
  digits.times { bin += rand(2).to_s(2) }
  bin
end

.decimal(l_digits: 5, r_digits: 2) ⇒ Float

Produces a float.

Examples:

Faker::Number.decimal(l_digits: 2) #=> 11.88
Faker::Number.decimal(l_digits: 3, r_digits: 3) #=> 181.843

Parameters:

  • l_digits (Integer) (defaults to: 5)

    Number of digits that the generated decimal should have to the left of the decimal point.

  • r_digits (Integer) (defaults to: 2)

    Number of digits that the generated decimal should have to the right of the decimal point.

Returns:

  • (Float)


69
70
71
72
73
74
75
76
77
# File 'lib/faker/default/number.rb', line 69

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

.decimal_part(digits: 10) ⇒ String

Produce a number with a number of digits, preserves leading zeroes.

Examples:

Faker::Number.decimal_part(digits: 2) #=> "09"

Parameters:

  • digits (Integer) (defaults to: 10)

    Number of digits that the generated number should have.

Returns:



48
49
50
51
52
53
54
55
# File 'lib/faker/default/number.rb', line 48

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

.digitInteger

Produces a single-digit integer.

Examples:

Faker::Number.digit #=> 1

Returns:

  • (Integer)


101
102
103
# File 'lib/faker/default/number.rb', line 101

def digit
  rand(10)
end

.hexadecimal(digits: 6) ⇒ String

Produces a number in hexadecimal format.

Examples:

Faker::Number.hexadecimal(digits: 3) #=> "e74"

Parameters:

  • digits (Integer) (defaults to: 6)

    Number of digits in the he

Returns:



115
116
117
118
119
# File 'lib/faker/default/number.rb', line 115

def hexadecimal(digits: 6)
  hex = ''
  digits.times { hex += rand(16).to_s(16) }
  hex
end

.leading_zero_number(digits: 10) ⇒ String

Produce a random number with a leading zero.

Examples:

Faker::Number.leading_zero_number(digits: 10) #=> "0669336915"

Parameters:

  • digits (Integer) (defaults to: 10)

    Number of digits that the generated number should have.

Returns:



34
35
36
# File 'lib/faker/default/number.rb', line 34

def leading_zero_number(digits: 10)
  "0#{(2..digits).collect { digit }.join}"
end

.negative(from: -5000.00,, to: -1.00)) ⇒ Float

Produces a negative float.

Examples:

Faker::Number.negative #=> -4480.042585669558

Parameters:

  • from (Integer) (defaults to: -5000.00,)

    The lower boundary.

  • to (Integer) (defaults to: -1.00))

    The higher boundary.

Returns:

  • (Float)


213
214
215
216
217
# File 'lib/faker/default/number.rb', line 213

def negative(from: -5000.00, to: -1.00)
  random_number = between(from: from, to: to)

  less_than_zero(random_number)
end

.non_zero_digitInteger

Produces a non-zero single-digit integer.

Examples:

Faker::Number.non_zero_digit #=> 8

Returns:

  • (Integer)


88
89
90
# File 'lib/faker/default/number.rb', line 88

def non_zero_digit
  rand(1..9)
end

.normal(mean: 1, standard_deviation: 1) ⇒ Float

Produces a float given a mean and standard deviation.

Examples:

Faker::Number.normal(mean: 50, standard_deviation: 3.5) #=> 47.14669604069156

Parameters:

  • mean (Integer) (defaults to: 1)
  • standard_deviation (Numeric) (defaults to: 1)

Returns:

  • (Float)


147
148
149
150
151
152
# File 'lib/faker/default/number.rb', line 147

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

.number(digits: 10) ⇒ Integer

Produce a random number.

Examples:

Faker::Number.number(digits: 10) #=> 1968353479

Parameters:

  • digits (Integer) (defaults to: 10)

    Number of digits that the generated number should have.

Returns:

  • (Integer)


16
17
18
19
20
21
22
# File 'lib/faker/default/number.rb', line 16

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

.positive(from: 1.00, to: 5000.00) ⇒ Float

Produces a positive float.

Examples:

Faker::Number.positive #=> 235.59238499107653

Parameters:

  • from (Integer) (defaults to: 1.00)

    The lower boundary.

  • to (Integer) (defaults to: 5000.00)

    The higher boundary.

Returns:

  • (Float)


196
197
198
199
200
# File 'lib/faker/default/number.rb', line 196

def positive(from: 1.00, to: 5000.00)
  random_number = between(from: from, to: to)

  greater_than_zero(random_number)
end

.within(range: 1.00..5000.00) ⇒ Numeric

Produces a number within two provided values. Boundaries are inclusive or exclusive depending on the range passed.

Examples:

Faker::Number.within(range: 1..10) #=> 7
Faker::Number.within(range: 0.0..1.0) #=> 0.7844640543957383

Parameters:

  • range (Range) (defaults to: 1.00..5000.00)

    The range from which to generate a number.

Returns:

  • (Numeric)


181
182
183
# File 'lib/faker/default/number.rb', line 181

def within(range: 1.00..5000.00)
  between(from: range.min, to: range.max)
end