NeoFaker.Number (neo_faker v0.14.0)

Copy Markdown View Source

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 negative integer between min and -1.

Generates a random positive integer between 1 and max.

Functions

between(min \\ 0, max \\ 100)

(since 0.8.0)
@spec between(number(), number()) :: number()

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 0100.

Parameters

  • min - Minimum value (inclusive). Defaults to 0.
  • max - Maximum value (inclusive). Defaults to 100.

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

decimal(min \\ 0.0, max \\ 100.0, precision \\ 2)

(since 0.8.0)
@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 to 0.0.
  • max - Maximum value (inclusive). Defaults to 100.0.
  • precision - Number of decimal places. Defaults to 2.

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

digit()

(since 0.8.0)
@spec digit() :: integer()

Generates a random single digit between 0 and 9.

Examples

iex> NeoFaker.Number.digit()
5

float(left_digit \\ %{__struct__: Range, first: 10, last: 100, step: 1}, right_digit \\ %{__struct__: Range, first: 10000, last: 100_000, step: 1})

(since 0.8.0)
@spec float(Range.t(), Range.t()) :: float()

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 to 10..100.
  • right_digit - Range for the fractional part. Defaults to 10_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

negative(min \\ -100)

(since 0.8.0)
@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

positive(max \\ 100)

(since 0.8.0)
@spec positive(pos_integer()) :: pos_integer()

Generates a random positive integer between 1 and max.

Parameters

  • max - Maximum value (inclusive). Defaults to 100.

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