NeoFaker.Number (neo_faker v0.15.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, inclusive.

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, inclusive.

Generates a random positive integer between 1 and max, inclusive.

Functions

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

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

Generates a random number between min and max, inclusive.

Returns an integer when both arguments are integers, or a float when either argument is a float. Defaults to the range 0–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.

min and max default to 0.0 and 100.0; precision 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 (defaults to 10..100) and a fractional part from right_digit (defaults to 10_000..100_000) into a float.

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, inclusive.

min 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, inclusive.

max 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