Functions for generating random numbers.
Provides utilities to generate random integers, floats, digits, and decimals, including values within a specified range and numbers with controlled precision.
Summary
Functions
Generates a random number between min and max.
Generates a random float rounded to the specified number of decimal places.
Generates a random single digit between 0 and 9.
Generates a random floating-point number within the given range.
Generates a random negative integer between min and -1.
Generates a random positive integer between 1 and max.
Functions
Generates a random number between min and max.
Returns an integer when both arguments are integers, or a float when either argument is a
float. Defaults to the range 0–100.
Parameters
min- Minimum value (inclusive). Defaults to0.max- Maximum value (inclusive). Defaults to100.
Examples
iex> NeoFaker.Number.between()
27
iex> NeoFaker.Number.between(1, 100)
28
iex> NeoFaker.Number.between(20, 100.0)
29.481745280074264
iex> NeoFaker.Number.between(50, 50)
50
iex> NeoFaker.Number.between(100, 1)
** (ArgumentError) min must be less than or equal to max, got: min=100, max=1
@spec decimal(number(), number(), non_neg_integer()) :: float()
Generates a random float rounded to the specified number of decimal places.
Parameters
min- Minimum value (inclusive). Defaults to0.0.max- Maximum value (inclusive). Defaults to100.0.precision- Number of decimal places. Defaults to2.
Examples
iex> NeoFaker.Number.decimal(0.0, 10.0, 2)
5.47
iex> NeoFaker.Number.decimal(0.0, 1.0, 4)
0.7384
iex> NeoFaker.Number.decimal()
42.73
iex> NeoFaker.Number.decimal(100.0, 0.0)
** (ArgumentError) min must be less than or equal to max, got: min=100.0, max=0.0
@spec digit() :: integer()
Generates a random single digit between 0 and 9.
Examples
iex> NeoFaker.Number.digit()
5
Generates a random floating-point number within the given range.
Combines a randomly selected integer part from left_digit and a fractional part from
right_digit into a float. Defaults to 10..100 for the integer part and
10_000..100_000 for the fractional part.
Parameters
left_digit- Range for the integer part. Defaults to10..100.right_digit- Range for the fractional part. Defaults to10_000..100_000.
Examples
iex> NeoFaker.Number.float()
30.94372
iex> NeoFaker.Number.float(1..9, 10..90)
1.44
iex> NeoFaker.Number.float(10..5, 10..100)
** (ArgumentError) left_digit range must have first <= last, got: 10..5
@spec negative(neg_integer()) :: neg_integer()
Generates a random negative integer between min and -1.
Parameters
min- Minimum value (inclusive). Defaults to-100.
Examples
iex> NeoFaker.Number.negative(-50)
-27
iex> NeoFaker.Number.negative()
-42
@spec positive(pos_integer()) :: pos_integer()
Generates a random positive integer between 1 and max.
Parameters
max- Maximum value (inclusive). Defaults to100.
Examples
iex> NeoFaker.Number.positive(50)
27
iex> NeoFaker.Number.positive()
42
iex> NeoFaker.Number.positive(0)
** (ArgumentError) max must be at least 1, got: 0