Skip to content

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