getRandomInt() Helper Method
The getRandomInt() Helper Method module provides a helper function that returns a random integer between a range of provided integers.
Import
To import the getRandomInt() Helper Method:
javascript
import { getRandomInt } from '@obewds/vueventus'
Arguments
Returns: Number
Args | Type | Status | Description |
---|---|---|---|
min | Number | Required | The minimum (inclusive) integer number to return as a possible random integer |
max | Number | Required | The maximum (inclusive) integer number to return as a possible random integer |
Use Example
javascript
console.log( getRandomInt( 0, 1 ) )
// returns (number): 0 or 1
console.log( getRandomInt( 1, 3 ) )
// returns (number): 1, 2 or 3
console.log( getRandomInt( 5, 10 ) )
// returns (number): 5, 6, 7, 8, 9 or 10
Module Code
ts
// ./src/helpers/getRandomInt.ts
export default function (min: number, max: number): number {
min = Math.ceil(min)
max = Math.floor(max)
// The maximum and minimum are both inclusive
return Math.floor(Math.random() * (max - min + 1) + min)
}