numz 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "numz",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "scientific computing with zikojs",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from './ufunc/index.js'
2
+ export * from './random/index.js'
3
+ export * from './complex/index.js'
2
4
  export * from './calculus/index.js'
3
5
  export * from './signal/index.js'
4
6
  export * from './stats/index.js'
5
- export * from './complex/index.js'
6
-
@@ -0,0 +1,162 @@
1
+ /**
2
+ * Utility class for generating random values.
3
+ */
4
+ export declare class Random {
5
+
6
+ /**
7
+ * Generates a random integer.
8
+ * @param a Minimum value (inclusive).
9
+ * @param b Maximum value (exclusive).
10
+ */
11
+ static int(a: number, b?: number): number;
12
+
13
+ /**
14
+ * Generates a random floating-point number.
15
+ * @param a Minimum value.
16
+ * @param b Maximum value.
17
+ */
18
+ static float(a: number, b?: number): number;
19
+
20
+ /**
21
+ * Generates a random binary value.
22
+ */
23
+ static bin(): 0 | 1;
24
+
25
+ /**
26
+ * Generates a random octal digit.
27
+ */
28
+ static oct(): number;
29
+
30
+ /**
31
+ * Generates a random decimal digit.
32
+ */
33
+ static dec(): number;
34
+
35
+ /**
36
+ * Generates a random hexadecimal digit.
37
+ */
38
+ static hex(): string;
39
+
40
+ /**
41
+ * Generates a random alphabetic character.
42
+ * @param upperCase Generate uppercase character when true.
43
+ */
44
+ static char(upperCase?: boolean): string;
45
+
46
+ /**
47
+ * Generates a random boolean value.
48
+ */
49
+ static bool(): boolean;
50
+
51
+ /**
52
+ * Random color generators.
53
+ */
54
+ static readonly color: {
55
+ /** Generates a random HEX color. */
56
+ hex(): string;
57
+
58
+ /** Generates a random HEXA color with alpha channel. */
59
+ hexa(): string;
60
+
61
+ /** Generates a random RGB color. */
62
+ rgb(): string;
63
+
64
+ /** Generates a random RGBA color. */
65
+ rgba(): string;
66
+
67
+ /** Generates a random HSL color. */
68
+ hsl(): string;
69
+
70
+ /** Generates a random HSLA color with alpha channel. */
71
+ hsla(): string;
72
+
73
+ /** Generates a random grayscale RGB color. */
74
+ gray(): string;
75
+ };
76
+
77
+ /**
78
+ * Generates arrays of random values.
79
+ */
80
+ static readonly sample: {
81
+
82
+ /**
83
+ * Generates an array of random integers.
84
+ */
85
+ int(n: number, a: number, b?: number): number[];
86
+
87
+ /**
88
+ * Generates an array of random floats.
89
+ */
90
+ float(n: number, a: number, b?: number): number[];
91
+
92
+ /**
93
+ * Generates an array of random characters.
94
+ */
95
+ char(n: number, upper?: boolean): string[];
96
+
97
+ /**
98
+ * Generates an array of random booleans.
99
+ */
100
+ bool(n: number): boolean[];
101
+
102
+ /**
103
+ * Generates an array of random binary values.
104
+ */
105
+ bin(n: number): (0 | 1)[];
106
+
107
+ /**
108
+ * Generates an array of random octal digits.
109
+ */
110
+ oct(n: number): number[];
111
+
112
+ /**
113
+ * Generates an array of random decimal digits.
114
+ */
115
+ dec(n: number): number[];
116
+
117
+ /**
118
+ * Generates an array of random hexadecimal digits.
119
+ */
120
+ hex(n: number): string[];
121
+
122
+ /**
123
+ * Generates arrays of random colors.
124
+ */
125
+ readonly color: {
126
+ hex(n: number): string[];
127
+ hexa(n: number): string[];
128
+ rgb(n: number): string[];
129
+ rgba(n: number): string[];
130
+ hsl(n: number): string[];
131
+ hsla(n: number): string[];
132
+ gray(n: number): string[];
133
+ };
134
+
135
+ /**
136
+ * Generates an array of random choices from a list.
137
+ * @param n Number of generated values.
138
+ * @param choices Available values.
139
+ * @param p Probability distribution for each value.
140
+ */
141
+ choice<T>(
142
+ n: number,
143
+ choices: T[],
144
+ p?: number[]
145
+ ): T[];
146
+ };
147
+
148
+ /**
149
+ * Returns a shuffled copy of an array.
150
+ */
151
+ static shuffle<T>(arr: T[]): T[];
152
+
153
+ /**
154
+ * Randomly selects a value from a list.
155
+ * @param choices Available values.
156
+ * @param p Probability distribution for each value.
157
+ */
158
+ static choice<T>(
159
+ choices?: T[],
160
+ p?: number[]
161
+ ): T;
162
+ }
@@ -0,0 +1,111 @@
1
+ // import { base2base } from "../../../dep/--from-ziko/functions/conversions/index.js";
2
+ import { accum_sum } from "../stats";
3
+
4
+ export class Random {
5
+ static int(a, b){
6
+ return Math.floor(this.float(a, b));
7
+ }
8
+ static float(a, b){
9
+ return b !== undefined
10
+ ? Math.random() * (b - a) + a
11
+ : Math.random() * a;
12
+ }
13
+ static bin(){
14
+ return this.int(2);
15
+ }
16
+ static oct(){
17
+ return this.int(8);
18
+ }
19
+ static dec(){
20
+ return this.int(10);
21
+ }
22
+ // static hex(){
23
+ // return base2base(this.int(16), 10, 16);
24
+ // }
25
+ static char(upperCase = false){
26
+ const i = upperCase
27
+ ? this.int(65, 91)
28
+ : this.int(97, 123);
29
+ return String.fromCharCode(i);
30
+ }
31
+ static bool(){
32
+ return Boolean(this.int(2));
33
+ }
34
+ static get color(){
35
+ return {
36
+ hex : () =>
37
+ `#${this.int(0xffffff).toString(16).padStart(6, '0')}`,
38
+
39
+ hexa : () => {
40
+ const [r,g,b,a] = Array.from(
41
+ {length:4},
42
+ () => this.int(0xff).toString(16).padStart(2,'0')
43
+ );
44
+ return `#${r}${g}${b}${a}`;
45
+ },
46
+ rgb : () => {
47
+ const [r,g,b] = Array.from({length:3}, () => this.int(0xff));
48
+ return `rgb(${r}, ${g}, ${b})`;
49
+ },
50
+ rgba : () => {
51
+ const [r,g,b] = Array.from({length:3}, () => this.int(0xff));
52
+ const a = Math.random().toFixed(2);
53
+ return `rgba(${r}, ${g}, ${b}, ${a})`;
54
+ },
55
+ hsl : () => {
56
+ const h = this.int(360);
57
+ const s = this.int(100);
58
+ const l = this.int(100);
59
+ return `hsl(${h}, ${s}%, ${l}%)`;
60
+ },
61
+ hsla : () => {
62
+ const h = this.int(360);
63
+ const s = this.int(100);
64
+ const l = this.int(100);
65
+ const a = Math.random().toFixed(2);
66
+ return `hsla(${h}, ${s}%, ${l}%, ${a})`;
67
+ },
68
+ gray : () => {
69
+ const g = this.int(0xff);
70
+ return `rgb(${g}, ${g}, ${g})`;
71
+ }
72
+ };
73
+ }
74
+ static get sample(){
75
+ const R = this;
76
+ return {
77
+ int : (n, a, b) => Array.from({length:n}, () => R.int(a, b)),
78
+ float : (n, a, b) => Array.from({length:n}, () => R.float(a, b)),
79
+ char : (n, upper=false) => Array.from({length:n}, () => R.char(upper)),
80
+ bool : n => Array.from({length:n}, () => R.bool()),
81
+ bin : n => Array.from({length:n}, () => R.bin()),
82
+ oct : n => Array.from({length:n}, () => R.oct()),
83
+ dec : n => Array.from({length:n}, () => R.dec()),
84
+ hex : n => Array.from({length:n}, () => R.hex()),
85
+ get color(){
86
+ return {
87
+ hex : n => Array.from({length:n}, () => R.color.hex()),
88
+ hexa : n => Array.from({length:n}, () => R.color.hexa()),
89
+ rgb : n => Array.from({length:n}, () => R.color.rgb()),
90
+ rgba : n => Array.from({length:n}, () => R.color.rgba()),
91
+ hsl : n => Array.from({length:n}, () => R.color.hsl()),
92
+ hsla : n => Array.from({length:n}, () => R.color.hsla()),
93
+ gray : n => Array.from({length:n}, () => R.color.gray())
94
+ };
95
+ },
96
+ choice : (n, choices, p) =>
97
+ Array.from({length:n}, () => R.choice(choices, p))
98
+ };
99
+ }
100
+ static shuffle(arr){
101
+ return [...arr].sort(() => 0.5 - Math.random());
102
+ }
103
+ // static choice(choices = [1,2,3], p = new Array(choices.length).fill(1 / choices.length)){
104
+ // const acc = accum_sum(...p).map(v => v * 100);
105
+ // const pool = new Array(100);
106
+ // pool.fill(choices[0], 0, acc[0]);
107
+ // for(let i=1;i<choices.length;i++)
108
+ // pool.fill(choices[i], acc[i-1], acc[i]);
109
+ // return pool[this.int(pool.length)];
110
+ // }
111
+ }