mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-27 14:57:44 +03:00
feat(random, randomInt): Accept singular argument
This commit is contained in:
parent
184ce1778d
commit
ff59ab4c17
@ -2,9 +2,12 @@
|
||||
|
||||
주어진 범위 내에서 무작위 숫자를 생성해요. 여기에서 숫자는 정수뿐만 아니라 소수점 있는 숫자도 포함해요.
|
||||
|
||||
인자가 하나만 주어지는 경우, `0`과 그 숫자 사이의 무작위 숫자를 반환해요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function random(maximum: number): number;
|
||||
function random(minimum: number, maximum: number): number;
|
||||
```
|
||||
|
||||
|
@ -2,9 +2,12 @@
|
||||
|
||||
주어진 범위 내에서 무작위 정수를 생성해요.
|
||||
|
||||
인자가 하나만 주어지는 경우, `0`과 그 숫자 사이의 무작위 숫자를 반환해요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function random(maximum: number): number;
|
||||
function randomInt(minimum: number, maximum: number): number;
|
||||
```
|
||||
|
||||
|
@ -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;
|
||||
```
|
||||
|
||||
|
@ -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;
|
||||
```
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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.');
|
||||
}
|
||||
|
@ -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!));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user