@chriscodesthings/random-in-range
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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...

Readme

Keywords

Package Sidebar

Install

npm i @chriscodesthings/random-in-range

Weekly Downloads

15

Version

1.0.0

License

MIT

Unpacked Size

6.08 kB

Total Files

8

Last publish

Collaborators

  • chriscodesthings