@chriscodesthings/random-in-range
v1.0.0
Published
Picks a random number within a given range
Downloads
32
Readme
random-in-range
Picks a random number within a given range
Description
Returns a random number within a given range.
See...
Install
npm install --save @chriscodesthings/random-in-range
Usage
import randomInRange from '@chriscodesthings/random-in-range';
console.log(randomInRange(1, 100));
// => 42
Syntax
randomInRange(n1, (n2));
Parameters
- n1: A number
- n2 (optional): A number
If n2 is omitted, the range is 0-n1, inclusive. If n2 is provided, the range is n1-n2 inclusive.
Return Value
Returns a random number in the range.
Examples
// range 0-n1
const securityQuestions = [
"Favourite colour",
"Favourite food",
"Favourite TV show",
// ....
];
function pickTwoQuestions(securityQuestions) {
const q1 = randomInRange(questions.length-1);
const q2 = randomInRange(questions.length-1);
return [questions[q1], questions[q2]];
}
// range n1-n2
function diceRoll() {
return randomInRange(1,6);
}