From 67cb5eff30d731123a9a832f782931603e057134 Mon Sep 17 00:00:00 2001 From: Sojin Park Date: Sat, 15 Jun 2024 16:00:20 +0900 Subject: [PATCH] docs(random, randomInt): Add docs for randomInt, and polish docs for random --- docs/.vitepress/en.mts | 2 ++ docs/.vitepress/ko.mts | 2 ++ docs/ko/reference/math/random.md | 6 ++---- docs/ko/reference/math/randomInt.md | 26 ++++++++++++++++++++++++++ docs/reference/math/random.md | 8 +++----- docs/reference/math/randomInt.md | 26 ++++++++++++++++++++++++++ src/math/random.spec.ts | 2 +- src/math/random.ts | 1 + 8 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 docs/ko/reference/math/randomInt.md create mode 100644 docs/reference/math/randomInt.md diff --git a/docs/.vitepress/en.mts b/docs/.vitepress/en.mts index 41f5edf2..da1f893c 100644 --- a/docs/.vitepress/en.mts +++ b/docs/.vitepress/en.mts @@ -92,6 +92,8 @@ function sidebar(): DefaultTheme.Sidebar { text: 'Math Utilities', items: [ { text: 'clamp', link: '/reference/math/clamp' }, + { text: 'random', link: '/reference/math/random' }, + { text: 'randomInt', link: '/reference/math/randomInt' }, { text: 'round', link: '/reference/math/round' }, { text: 'sum', link: '/reference/math/sum' }, ], diff --git a/docs/.vitepress/ko.mts b/docs/.vitepress/ko.mts index ef907c58..cbb243d4 100644 --- a/docs/.vitepress/ko.mts +++ b/docs/.vitepress/ko.mts @@ -91,6 +91,8 @@ function sidebar(): DefaultTheme.Sidebar { text: '숫자', items: [ { text: 'clamp', link: '/ko/reference/math/clamp' }, + { text: 'random', link: '/ko/reference/math/random' }, + { text: 'randomInt', link: '/ko/reference/math/randomInt' }, { text: 'round', link: '/ko/reference/math/round' }, { text: 'sum', link: '/ko/reference/math/sum' }, ], diff --git a/docs/ko/reference/math/random.md b/docs/ko/reference/math/random.md index 54ef3c56..a52e635a 100644 --- a/docs/ko/reference/math/random.md +++ b/docs/ko/reference/math/random.md @@ -1,8 +1,6 @@ # random -주어진 범위 내에서 무작위 부동 소수점 숫자를 생성하는 함수에요. - -이 함수는 최솟값과 최댓값을 받아서, 해당 범위 내에서 무작위 부동 소수점 숫자를 반환해요. +주어진 범위 내에서 무작위 숫자를 생성해요. 여기에서 숫자는 정수뿐만 아니라 소수점 있는 숫자도 포함해요. ## 인터페이스 @@ -17,7 +15,7 @@ function random(minimum: number, maximum: number): number; ### 반환 값 -- (`number`): 지정된 범위 내에서 무작위 부동 소수점 숫자를 반환해요. +- (`number`): 지정된 범위 내에서 무작위 숫자를 반환해요. 숫자는 정수뿐만 아니라 소수점 있는 숫자도 포함해요. ## 예시 diff --git a/docs/ko/reference/math/randomInt.md b/docs/ko/reference/math/randomInt.md new file mode 100644 index 00000000..bee52c02 --- /dev/null +++ b/docs/ko/reference/math/randomInt.md @@ -0,0 +1,26 @@ +# randomInt + +주어진 범위 내에서 무작위 정수를 생성해요. + +## 인터페이스 + +```typescript +function randomInt(minimum: number, maximum: number): number; +``` + +### 파라미터 + +- `minimum` (`number`): 무작위 정수를 생성할 최솟값(포함)이에요. +- `maximum` (`number`): 무작위 정수를 생성할 최댓값(미포함)이에요. + +### 반환 값 + +- (`number`): 지정된 범위 내에서 무작위 정수를 반환해요. + +## 예시 + +```typescript +const result1 = randomInt(0, 5); // 0과 5사이의 무작위 정수를 반환해요. +const result2 = randomInt(5, 0); // 최솟값이 최댓값보다 크면 오류가 발생해요. +const result3 = randomInt(5, 5); // 최솟값이 최댓값과 같으면 오류가 발생해요. +``` diff --git a/docs/reference/math/random.md b/docs/reference/math/random.md index 905f1bf8..6eb4dd52 100644 --- a/docs/reference/math/random.md +++ b/docs/reference/math/random.md @@ -1,8 +1,6 @@ # random -Generates a random floating-point number within a given range. - -This function takes a minimum and maximum value, and returns a random floating-point number within that range. +Generate a random number within the given range. The number can be an integer or a decimal. ## Signature @@ -17,12 +15,12 @@ function random(minimum: number, maximum: number): number; ### Returns -- (`number`): A random floating-point number within the specified range. +- (`number`): A random number within the specified range. The number can be an integer or a decimal. ## Examples ```typescript -const result1 = random(0, 5); // Returns a random floating-point number between 0 and 5. +const result1 = random(0, 5); // Returns a random number between 0 and 5. 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. ``` diff --git a/docs/reference/math/randomInt.md b/docs/reference/math/randomInt.md new file mode 100644 index 00000000..d646a1d3 --- /dev/null +++ b/docs/reference/math/randomInt.md @@ -0,0 +1,26 @@ +# randomInt + +Generate a random integer within the given range. + +## Signature + +```typescript +function randomInt(minimum: number, maximum: number): number; +``` + +### Parameters + +- `minimum` (`number`): The lower bound for the random integer (inclusive). +- `maximum` (`number`): The upper bound for the random integer (exclusive). + +### Returns + +- (`number`): A random integer within the specified range. + +## Examples + +```typescript +const result1 = randomInt(0, 5); // Returns a random integer between 0 and 5. +const result2 = randomInt(5, 0); // If the minimum is greater than the maximum, an error is thrown +const result3 = randomInt(5, 5); // If the minimum is equal to the maximum, an error is thrown. +``` diff --git a/src/math/random.spec.ts b/src/math/random.spec.ts index 1b0dfb56..4df3224b 100644 --- a/src/math/random.spec.ts +++ b/src/math/random.spec.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest'; import { random } from './random'; -describe('random function', () => { +describe('random', () => { it('generates a random floating-point number between min (inclusive) and max (exclusive)', () => { const min = 0; const max = 5; diff --git a/src/math/random.ts b/src/math/random.ts index ed03a465..b56338d8 100644 --- a/src/math/random.ts +++ b/src/math/random.ts @@ -5,6 +5,7 @@ * @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). + * @throws {Error} Throws an error if `maximum` is not greater than `minimum`. * * @example * const result = random(0, 5); // result will be a random floating-point number between 0 (inclusive) and 5 (exclusive)