Random
Generate a random number between two numbers.
๐ฌ Usage
<script>
import {random} from "@sveu/shared/math"
let result = ""
</script>
<h1>{result}</h1>
<button on:click="{() => result = random(0, 100)}">Generate</button>
๐ฉโ๐ปAPI
๐ป Arguments
Name | Description | Type | Required |
---|---|---|---|
min | The minimum number to generate. | number | Yes |
max | The maximum number to generate. | number | Yes |
โฉ๏ธ Returns
Returns a random number between the minimum and maximum number.
๐งช Playground
Source Code ๐
Source Code
/**
* Returns a random number between min (inclusive) and max (exclusive)
*
* @param min - The minimum number
*
* @param max - The maximum number
*
* @returns a random number between min (inclusive) and max (exclusive).
*/
export function random(min: number, max: number): number {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1)) + min
}
Last update: 2023-02-14
Authors: Mohamed-Kaizen