1.0.0 • Published 1 year ago

@chriscodesthings/random-in-range v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

random-in-range Test workflow status NPM Version License: MIT

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);
}

See Also...