Skip to content
  • Sponsor
  • Notifications You must be signed in to change notification settings
  • Fork 0

Commit 5cd58b1

Browse files
authoredOct 25, 2024··
feat: add item function (#265)
1 parent 9240528 commit 5cd58b1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed
 

‎workspace/mauss/src/random/index.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import type { IndexSignature, TypedIntArray } from '../typings/aliases.js';
22

3-
/** Generates a random floating point number between `min` and `max` */
3+
/**
4+
* Generates a random floating point number between `min` and `max`
5+
* @default max=1
6+
* @default min=0
7+
*/
48
export function float(max = 1, min = 0): number {
59
[min, max] = [Math.ceil(min), Math.floor(max)];
610
return Math.random() * (max - min) + min;
711
}
812

9-
/** Generates a random integer between `min` and `max` */
13+
/**
14+
* Generates a random integer between `min` and `max`
15+
* @default max=1
16+
* @default min=0
17+
*/
1018
export function int(max = 1, min = 0): number {
1119
return Math.floor(float(max, min));
1220
}
@@ -33,6 +41,11 @@ export function val<T>(dict: Record<IndexSignature, T>): T {
3341
return values[int(values.length)];
3442
}
3543

44+
/** Gets a random item from a list */
45+
export function item<T>(list: T[]): T {
46+
return list[int(list.length)];
47+
}
48+
3649
/** Generates a random hexadecimal color code */
3750
export function hex(): string {
3851
return `#${~~(float() * (1 << 24)).toString(16)}`;

0 commit comments

Comments
 (0)
Please sign in to comment.