Toolbox-XToolbox-X

Get Random Float

Generates a random floating-point number within a specified range.

getRandomFloat

The getRandomFloat function generates a random floating-point number within a specified range (inclusive of min, exclusive of max). It's useful for simulations, statistical sampling, and any scenario requiring precise random decimal values.

Function Signature

function getRandomFloat(min: Numeric, max: Numeric): number

Parameters

  • min (Numeric): The lower bound (inclusive) - can be a number or numeric string.
  • max (Numeric): The upper bound (exclusive) - can be a number or numeric string.

Return Value

  • number: A random floating-point number where min ≤ value < max.

Example Usage

playground.ts

Notes

  • Inclusive/Exclusive: The minimum is inclusive, while the maximum is exclusive (matching native Math.random() behavior).
  • Numeric Conversion: Automatically converts numeric strings to numbers.
  • Edge Cases: If min equals max, it returns min. If min > max, the values are swapped internally.

Types

Numeric

type Numeric = number | `${number}`;

Aliases

  • getRandomDecimal

Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)

On this page