Class: Faker::Number
Constant Summary
Constants inherited from Base
Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters
Class Method Summary collapse
-
.between(from: 1.00, to: 5000.00) ⇒ Numeric
Produces a number between two provided values.
-
.binary(digits: 4) ⇒ String
Produces a number in binary format.
-
.decimal(l_digits: 5, r_digits: 2) ⇒ Float
Produces a float.
-
.decimal_part(digits: 10) ⇒ String
Produce a number with a number of digits, preserves leading zeroes.
-
.digit ⇒ Integer
Produces a single-digit integer.
-
.hexadecimal(digits: 6) ⇒ String
Produces a number in hexadecimal format.
-
.leading_zero_number(digits: 10) ⇒ String
Produce a random number with a leading zero.
-
.negative(from: -5000.00,, to: -1.00)) ⇒ Float
Produces a negative float.
-
.non_zero_digit ⇒ Integer
Produces a non-zero single-digit integer.
-
.normal(mean: 1, standard_deviation: 1) ⇒ Float
Produces a float given a mean and standard deviation.
-
.number(digits: 10) ⇒ Integer
Produce a random number.
-
.positive(from: 1.00, to: 5000.00) ⇒ Float
Produces a positive float.
-
.within(range: 1.00..5000.00) ⇒ Numeric
Produces a number within two provided values.
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.
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.
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.
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.
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 |
.digit ⇒ Integer
Produces a single-digit 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.
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.
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.
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_digit ⇒ Integer
Produces a non-zero single-digit 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.
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.
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.
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.
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 |