diff --git a/docs/ko/reference/math/random.md b/docs/ko/reference/math/random.md index a52e635a..2f7f8bd4 100644 --- a/docs/ko/reference/math/random.md +++ b/docs/ko/reference/math/random.md @@ -2,9 +2,12 @@ 주어진 범위 내에서 무작위 숫자를 생성해요. 여기에서 숫자는 정수뿐만 아니라 소수점 있는 숫자도 포함해요. +인자가 하나만 주어지는 경우, `0`과 그 숫자 사이의 무작위 숫자를 반환해요. + ## 인터페이스 ```typescript +function random(maximum: number): number; function random(minimum: number, maximum: number): number; ``` diff --git a/docs/ko/reference/math/randomInt.md b/docs/ko/reference/math/randomInt.md index bee52c02..0bf82bc4 100644 --- a/docs/ko/reference/math/randomInt.md +++ b/docs/ko/reference/math/randomInt.md @@ -2,9 +2,12 @@ 주어진 범위 내에서 무작위 정수를 생성해요. +인자가 하나만 주어지는 경우, `0`과 그 숫자 사이의 무작위 숫자를 반환해요. + ## 인터페이스 ```typescript +function random(maximum: number): number; function randomInt(minimum: number, maximum: number): number; ``` diff --git a/docs/reference/math/random.md b/docs/reference/math/random.md index 6eb4dd52..d59a65d2 100644 --- a/docs/reference/math/random.md +++ b/docs/reference/math/random.md @@ -2,9 +2,12 @@ Generate a random number within the given range. The number can be an integer or a decimal. +If only one argument is provided, a number between `0` and the given number is returned. + ## Signature ```typescript +function random(maximum: number): number; function random(minimum: number, maximum: number): number; ``` diff --git a/docs/reference/math/randomInt.md b/docs/reference/math/randomInt.md index d646a1d3..19fa6e21 100644 --- a/docs/reference/math/randomInt.md +++ b/docs/reference/math/randomInt.md @@ -2,9 +2,12 @@ Generate a random integer within the given range. +If only one argument is provided, a number between `0` and the given number is returned. + ## Signature ```typescript +function randomInt(maximum: number): number; function randomInt(minimum: number, maximum: number): number; ``` diff --git a/src/math/random.spec.ts b/src/math/random.spec.ts index 4df3224b..e9261e5d 100644 --- a/src/math/random.spec.ts +++ b/src/math/random.spec.ts @@ -12,6 +12,14 @@ describe('random', () => { } }); + it('generates a random floating-point between 0 and max (exclusive)', () => { + for (let i = 0; i < 100; i++) { + const result = random(5); + expect(result).toBeGreaterThanOrEqual(0); + expect(result).toBeLessThan(5); + } + }); + it('throws an error if min is greater than max', () => { const min = 5; const max = 0; diff --git a/src/math/random.ts b/src/math/random.ts index 5056e168..84d560e1 100644 --- a/src/math/random.ts +++ b/src/math/random.ts @@ -1,6 +1,8 @@ /** * Generate a random number within the given range. * + * If only one argument is provided, a number between `0` and the given number is returned. + * * @param {number} minimum - The lower bound (inclusive). * @param {number} maximum - The upper bound (exclusive). * @returns {number} A random number between minimum (inclusive) and maximum (exclusive). The number can be an integer or a decimal. @@ -11,7 +13,14 @@ * const result2 = random(5, 0); // If the minimum is greater than the maximum, an error is thrown * const result3 = random(5, 5); // If the minimum is equal to the maximum, an error is thrown. */ -export function random(minimum: number, maximum: number): number { +export function random(maximum: number): number; +export function random(minimum: number, maximum: number): number; +export function random(minimum: number, maximum?: number): number { + if (maximum == null) { + maximum = minimum; + minimum = 0; + } + if (minimum >= maximum) { throw new Error('Invalid input: The maximum value must be greater than the minimum value.'); } diff --git a/src/math/randomInt.ts b/src/math/randomInt.ts index fd30f743..b664fac5 100644 --- a/src/math/randomInt.ts +++ b/src/math/randomInt.ts @@ -3,6 +3,8 @@ import { random } from './random'; /** * Generates a random integer between minimum (inclusive) and maximum (exclusive). * + * If only one argument is provided, a number between `0` and the given number is returned. + * * @param {number} minimum - The lower bound (inclusive). * @param {number} maximum - The upper bound (exclusive). * @returns {number} A random integer between minimum (inclusive) and maximum (exclusive). @@ -12,6 +14,8 @@ import { random } from './random'; * const result = randomInt(0, 5); // result will be a random integer between 0 (inclusive) and 5 (exclusive) * const result2 = randomInt(5, 0); // This will throw an error */ -export function randomInt(minimum: number, maximum: number): number { - return Math.floor(random(minimum, maximum)); +export function randomInt(maximum: number): number; +export function randomInt(minimum: number, maximum: number): number; +export function randomInt(minimum: number, maximum?: number): number { + return Math.floor(random(minimum, maximum!)); }