A JS toolkit with random numbers, random time, random human elements, and more
npm i js-util-random
const random = require('js-util-random'); // CommonJS
import random from 'js-util-random'; // ES6 module
random.random(); // 5
random.random(0, 100); // 42
random.random(0, 100, 2); // 42.42
random.randomWithWeight([
{ value: 'a', weight: 1 },
{ value: 'b', weight: 2 },
{ value: 'c', weight: 9 },
]); // 'c'
Generates a random number between min
and max
with accuracy
decimal places.
min
and max
default to 0
and 9
respectively.
accuracy
defaults to 0
.
Examples:
random(); // 5
random(0, 100); // 42
random(0, 100, 2); // 42.42
Generates a random item from items
based on the weight of each item.
items
is an array of objects with value
and weight
properties.
Example:
randomWithWeight([
{ value: 'a', weight: 1 },
{ value: 'b', weight: 2 },
{ value: 'c', weight: 9 },
]); // 'c'
Generates human age random number between min
and max
.
min
and max
default to 0
and 120
respectively.
Example:
randomHumanAge(); // 5
randomHumanAge(0, 100); // 42
Generates random human gender.
maleAlias
and femaleAlias
default to 0
and 1
respectively.
Example:
randomHumanGender(); // 0
randomHumanGender('男', '女'); // 男
Generates random boolean.
Example:
randomBoolean(); // true
Generates random chinese name.
isFemale
default to false
.
Example:
randomHumanChineseName(); // '张三'
randomHumanChineseName(true); // '李四'
Generates random date time.
start
and end
default to new Date(1970, 0, 1)
and new Date()
respectively.
Example:
randomDateTime(); // '2019-01-01T00:00:00.000Z'
Generates random date.
start
and end
default to new Date(1970, 0, 1)
and new Date()
respectively.
Example:
randomDate(); // '2019-01-01'
Generates random time.
start
and end
default to new Date(1970, 0, 1)
and new Date()
respectively.
Example:
randomTime(); // '00:00:00'
Generates random date time string.
start
and end
default to new Date(1970, 0, 1)
and new Date()
respectively.
Example:
randomDateTimeString(); // 2020/7/22 01:10:15'
Generates random date string.
start
and end
default to new Date(1970, 0, 1)
and new Date()
respectively.
Example:
randomDateString(); // '2020/7/22'
Generates random time string.
start
and end
default to new Date(1970, 0, 1)
and new Date()
respectively.
Example:
randomTimeString(); // '01:10:15'
Generates random timestamp.
start
and end
default to new Date(1970, 0, 1)
and new Date()
respectively.
Example:
randomTimeStamp(); // 1595364615000
Generates random char.
chars
default to 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
.
Example:
randomChar(); // 'a'
MIT License