Random all the things
npm install utils.random
const random = require('utils.random');
import random from 'utils.random';
You can use it directly in your browser using unpkg.com:
<script src="https://unpkg.com/[email protected]/dist/index.min.js"></script>
import random from 'utils.random';
// Generate a random float between 0 and 1
random();
// => 0.28126727278265307
// Generate a random integer with a max value of 100
random(100);
// => 57
// Generate a random integer between 50 and 100
random(50, 100);
// => 77
// Pick a random item from an array
random([1, 2, 3, 4, 5, 'random', 'words']);
// => 4
// Pick N number of items from an array
random([1, 2, 3, 4, 5, 'random', 'words'], 3);
// => ['words', 4, 1]
Returns a float between 0 and 1. (It's the same as Math.random()
)
Returns an integer between 0
and max
.
Returns and integer between min
and max
Returns a random item from array
Returns a new array of length size
with randomly selected items from array
. If size is equal to or larger than array.length
, it returns a shuffled copy of the array
.
Type: number
Default: 0
Minimum integer to return
Type: number
Maximum integer to return
Type: array
Array to pick random items from
Type: number
The number of items to pick from array
npm install
npm test
MIT © Zsolt Meszaros