Picks a random number within a given range
Returns a random number within a given range.
npm install --save @chriscodesthings/random-in-range
import randomInRange from '@chriscodesthings/random-in-range';
console.log(randomInRange(1, 100));
// => 42
randomInRange(n1, (n2));
- 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.
Returns a random number in the range.
// 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);
}