rnd
- Description:
- A util lib for generating various types of random data. It provides functions to generate random integers, floats, booleans, colors, and strings Uses Math.random(), so it is NOT cryptographically secure. For security-sensitive randomness, use the 'crypto' module.
Methods
(static) bool() → {boolean}
- Description:
- Returns a random boolean (true or false).
Example
const randomBool = rnd.bool(); // Returns either true or false
console.log(randomBool); // Outputs a random boolean
Returns:
- Type
- boolean
(static) choice(array) → {any|undefined}
- Description:
- Selects a random element from an array.
Examples
const randomChoice = rnd.choice([1, 2, 3, 4, 5]); // Returns a random element from the array
console.log(randomChoice); // Outputs a random element from the array
const randomChoice = rnd.choice([]); // Returns undefined
Parameters:
Name | Type | Description |
---|---|---|
array |
Array.<any> | The array to choose from. |
Returns:
A random element from the array, or undefined if the array is empty.
- Type
- any | undefined
(static) color() → {string}
- Description:
- Generates a random hex color code.
Example
const randomColor = rnd.color(); // Returns a random hex color code
console.log(randomColor); // Outputs a random hex color code
Returns:
A random hex color string (e.g., '#a4c1e8').
- Type
- string
(static) float(max) → {number}
- Description:
- Generates a random float from 0 up to (but not including) max.
Example
const randomFloat = rnd.float(10); // Returns a random float between 0 and 10
console.log(randomFloat); // Outputs a random float
Parameters:
Name | Type | Description |
---|---|---|
max |
number | The upper bound (exclusive). |
Throws:
-
If max is not a positive number.
- Type
- Error
Returns:
A random float.
- Type
- number
(static) floatInRange(min, max) → {number}
- Description:
- Generates a random float within a given range.
Example
const randomFloat = rnd.floatInRange(1.5, 5.5); // Returns a random float between 1.5 and 5.5
console.log(randomFloat); // Outputs a random float
Parameters:
Name | Type | Description |
---|---|---|
min |
number | The minimum value of the range. |
max |
number | The maximum value of the range. |
Throws:
-
If min is greater than max.
- Type
- Error
Returns:
A random float.
- Type
- number
(static) int(max) → {number}
- Description:
- Generates a random integer from 0 up to (but not including) max.
Example
const randomInt = rnd.int(10); // Returns a random integer between 0 and 9
console.log(randomInt); // Outputs a random integer
Parameters:
Name | Type | Description |
---|---|---|
max |
number | The upper bound (exclusive). |
Throws:
-
If max is not a positive number.
- Type
- Error
Returns:
A random integer.
- Type
- number
(static) intInRange(min, max) → {number}
- Description:
- Generates a random integer within a given range (inclusive).
Example
const randomInt = rnd.intInRange(1, 10); // Returns a random integer between 1 and 10
console.log(randomInt); // Outputs a random integer
Parameters:
Name | Type | Description |
---|---|---|
min |
number | The minimum value of the range. |
max |
number | The maximum value of the range. |
Throws:
-
If min is greater than max.
- Type
- Error
Returns:
A random integer.
- Type
- number
(static) shuffle(array) → {Array.<any>}
- Description:
- Shuffles an array in place using the Fisher-Yates algorithm and returns it.
Example
const shuffledArray = rnd.shuffle([1, 2, 3, 4, 5]); // Returns a shuffled version of the array
console.log(shuffledArray); // Outputs the shuffled array
Parameters:
Name | Type | Description |
---|---|---|
array |
Array.<any> | The array to shuffle. |
Returns:
The shuffled array.
- Type
- Array.<any>
(static) string(length) → {string}
- Description:
- Generates a random string of a given length using only alphabetic characters. NOT cryptographically secure. For secure random strings, use the 'crypto' module.
Example
const randomString = rnd.string(10); // Returns a random string of 10 characters
console.log(randomString); // Outputs a random string of letters
Parameters:
Name | Type | Description |
---|---|---|
length |
number | The desired length of the string. |
Returns:
A random string of letters.
- Type
- string