File tree 1 file changed +15
-2
lines changed
workspace/mauss/src/random
1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change 1
1
import type { IndexSignature , TypedIntArray } from '../typings/aliases.js' ;
2
2
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
+ */
4
8
export function float ( max = 1 , min = 0 ) : number {
5
9
[ min , max ] = [ Math . ceil ( min ) , Math . floor ( max ) ] ;
6
10
return Math . random ( ) * ( max - min ) + min ;
7
11
}
8
12
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
+ */
10
18
export function int ( max = 1 , min = 0 ) : number {
11
19
return Math . floor ( float ( max , min ) ) ;
12
20
}
@@ -33,6 +41,11 @@ export function val<T>(dict: Record<IndexSignature, T>): T {
33
41
return values [ int ( values . length ) ] ;
34
42
}
35
43
44
+ /** Gets a random item from a list */
45
+ export function item < T > ( list : T [ ] ) : T {
46
+ return list [ int ( list . length ) ] ;
47
+ }
48
+
36
49
/** Generates a random hexadecimal color code */
37
50
export function hex ( ) : string {
38
51
return `#${ ~ ~ ( float ( ) * ( 1 << 24 ) ) . toString ( 16 ) } ` ;
You can’t perform that action at this time.
0 commit comments