Get Random Float
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
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.
Type: Numeric
A union type representing either a number or a numeric string (e.g., "3.14"):
type Numeric = number | `${number}`;
Return Value
Returns a random floating-point number where:
min ≤ value < max
Example Usage
Basic Usage
import { getRandomFloat } from 'nhb-toolbox';
// Returns a float between 1.5 (inclusive) and 3.5 (exclusive)
console.log(getRandomFloat(1.5, 3.5)); // e.g. 2.84623
With Numeric Strings
console.log(getRandomFloat("0.1", "0.5")); // e.g. 0.37281
Negative Ranges
console.log(getRandomFloat(-2.5, -1.0)); // e.g. -1.76342
Notes
- Inclusive/Exclusive: The minimum is inclusive, while the maximum is exclusive (matches
Math.random()behavior). - Numeric Conversion: Automatically converts numeric strings to numbers.
- Edge Cases: If
minequalsmax, always returnsmin. Ifmin>max, the values are effectively swapped. - Precision: Uses JavaScript's native
Math.random()precision (typically 53 bits).
Aliases
getRandomDecimal: Alias forgetRandomFloat.
Common Use Cases
- Monte Carlo simulations
- Random sampling in statistical analysis
- Game development (physics, damage values)
- Generative art algorithms
- Machine learning weight initialization
Conclusion
The getRandomFloat function provides a convenient way to generate precise random decimal numbers within any range, with support for both number and string inputs. Its alias getRandomDecimal offers semantic flexibility depending on context.