randomInt
Generate a random integer in a range.
Demo
vue
<script setup lang="ts">
import { randomInt } from '.'
const data = ref(-1)
</script>
<template>
<div>
<button btn @click="data = randomInt(0, 100)">
Roll!
</button>
<p>{{ data }}</p>
</div>
</template>-1
Type Declarations
Details
ts
/**
* Closed interval random integer number
*
* @example
*
* ```js
* // 1 <= value <= 10
* randomInt(1, 10)
* // A rand function return [0, 1) to replace `Math.random`
* randomInt(1, 10, () => myRand())
* ```
*
* ## Prove
*
* 1. rand() return value -> [0, 1)
*
* 2. rand() * (high - low + 1) return value -> [0, high - low + 1)
*
* 3. Floor it return value -> [0, high - low].
*
* When floor it, value is 0 -> [0, 1), 1 -> [0, 2), ... , Max -> [Max-1, Max),
* and because Math.random return a uniform distribution number,
* so the rate is still uniform distribution.
*
* 4. plus low -> [low, high]
*
* @see {@link https://s3xysteak.github.io/fnclip/functions/number/randomInt/}
*/
export declare function randomInt(min: number, max: number, rand?: () => number): number;