@elaraai/east-node-std 0.0.1-beta.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.
Files changed (101) hide show
  1. package/CLA.md +26 -0
  2. package/CONTRIBUTING.md +28 -0
  3. package/README.md +135 -0
  4. package/dist/console.d.ts +192 -0
  5. package/dist/console.d.ts.map +1 -0
  6. package/dist/console.js +228 -0
  7. package/dist/console.js.map +1 -0
  8. package/dist/crypto.d.ts +212 -0
  9. package/dist/crypto.d.ts.map +1 -0
  10. package/dist/crypto.js +259 -0
  11. package/dist/crypto.js.map +1 -0
  12. package/dist/fetch.d.ts +310 -0
  13. package/dist/fetch.d.ts.map +1 -0
  14. package/dist/fetch.js +336 -0
  15. package/dist/fetch.js.map +1 -0
  16. package/dist/fs.d.ts +520 -0
  17. package/dist/fs.d.ts.map +1 -0
  18. package/dist/fs.js +630 -0
  19. package/dist/fs.js.map +1 -0
  20. package/dist/index.d.ts +59 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +86 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/path.d.ts +241 -0
  25. package/dist/path.d.ts.map +1 -0
  26. package/dist/path.js +299 -0
  27. package/dist/path.js.map +1 -0
  28. package/dist/random/crypto-rng.d.ts +31 -0
  29. package/dist/random/crypto-rng.d.ts.map +1 -0
  30. package/dist/random/crypto-rng.js +44 -0
  31. package/dist/random/crypto-rng.js.map +1 -0
  32. package/dist/random/distributions/bates.d.ts +12 -0
  33. package/dist/random/distributions/bates.d.ts.map +1 -0
  34. package/dist/random/distributions/bates.js +15 -0
  35. package/dist/random/distributions/bates.js.map +1 -0
  36. package/dist/random/distributions/bernoulli.d.ts +12 -0
  37. package/dist/random/distributions/bernoulli.d.ts.map +1 -0
  38. package/dist/random/distributions/bernoulli.js +13 -0
  39. package/dist/random/distributions/bernoulli.js.map +1 -0
  40. package/dist/random/distributions/binomial.d.ts +12 -0
  41. package/dist/random/distributions/binomial.d.ts.map +1 -0
  42. package/dist/random/distributions/binomial.js +24 -0
  43. package/dist/random/distributions/binomial.js.map +1 -0
  44. package/dist/random/distributions/exponential.d.ts +12 -0
  45. package/dist/random/distributions/exponential.d.ts.map +1 -0
  46. package/dist/random/distributions/exponential.js +13 -0
  47. package/dist/random/distributions/exponential.js.map +1 -0
  48. package/dist/random/distributions/geometric.d.ts +12 -0
  49. package/dist/random/distributions/geometric.d.ts.map +1 -0
  50. package/dist/random/distributions/geometric.js +14 -0
  51. package/dist/random/distributions/geometric.js.map +1 -0
  52. package/dist/random/distributions/irwin-hall.d.ts +12 -0
  53. package/dist/random/distributions/irwin-hall.d.ts.map +1 -0
  54. package/dist/random/distributions/irwin-hall.js +18 -0
  55. package/dist/random/distributions/irwin-hall.js.map +1 -0
  56. package/dist/random/distributions/log-normal.d.ts +12 -0
  57. package/dist/random/distributions/log-normal.d.ts.map +1 -0
  58. package/dist/random/distributions/log-normal.js +12 -0
  59. package/dist/random/distributions/log-normal.js.map +1 -0
  60. package/dist/random/distributions/normal.d.ts +13 -0
  61. package/dist/random/distributions/normal.d.ts.map +1 -0
  62. package/dist/random/distributions/normal.js +16 -0
  63. package/dist/random/distributions/normal.js.map +1 -0
  64. package/dist/random/distributions/pareto.d.ts +12 -0
  65. package/dist/random/distributions/pareto.d.ts.map +1 -0
  66. package/dist/random/distributions/pareto.js +14 -0
  67. package/dist/random/distributions/pareto.js.map +1 -0
  68. package/dist/random/distributions/poisson.d.ts +12 -0
  69. package/dist/random/distributions/poisson.d.ts.map +1 -0
  70. package/dist/random/distributions/poisson.js +81 -0
  71. package/dist/random/distributions/poisson.js.map +1 -0
  72. package/dist/random/distributions/uniform-int.d.ts +12 -0
  73. package/dist/random/distributions/uniform-int.d.ts.map +1 -0
  74. package/dist/random/distributions/uniform-int.js +16 -0
  75. package/dist/random/distributions/uniform-int.js.map +1 -0
  76. package/dist/random/distributions/uniform.d.ts +12 -0
  77. package/dist/random/distributions/uniform.d.ts.map +1 -0
  78. package/dist/random/distributions/uniform.js +10 -0
  79. package/dist/random/distributions/uniform.js.map +1 -0
  80. package/dist/random/rng.d.ts +17 -0
  81. package/dist/random/rng.d.ts.map +1 -0
  82. package/dist/random/rng.js +7 -0
  83. package/dist/random/rng.js.map +1 -0
  84. package/dist/random/xorshift128.d.ts +35 -0
  85. package/dist/random/xorshift128.d.ts.map +1 -0
  86. package/dist/random/xorshift128.js +112 -0
  87. package/dist/random/xorshift128.js.map +1 -0
  88. package/dist/random.d.ts +773 -0
  89. package/dist/random.d.ts.map +1 -0
  90. package/dist/random.js +934 -0
  91. package/dist/random.js.map +1 -0
  92. package/dist/test.d.ts +253 -0
  93. package/dist/test.d.ts.map +1 -0
  94. package/dist/test.js +407 -0
  95. package/dist/test.js.map +1 -0
  96. package/dist/time.d.ts +130 -0
  97. package/dist/time.d.ts.map +1 -0
  98. package/dist/time.js +154 -0
  99. package/dist/time.js.map +1 -0
  100. package/dist/tsconfig.tsbuildinfo +1 -0
  101. package/package.json +84 -0
package/dist/random.js ADDED
@@ -0,0 +1,934 @@
1
+ /**
2
+ * Copyright (c) 2025 Elara AI Pty Ltd
3
+ * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
4
+ */
5
+ import { East, FloatType, IntegerType, NullType } from "@elaraai/east";
6
+ import { EastError } from "@elaraai/east/internal";
7
+ import CryptoRNG from "./random/crypto-rng.js";
8
+ import XorShift128RNG from "./random/xorshift128.js";
9
+ import uniformDist from "./random/distributions/uniform.js";
10
+ import uniformIntDist from "./random/distributions/uniform-int.js";
11
+ import normalDist from "./random/distributions/normal.js";
12
+ import exponentialDist from "./random/distributions/exponential.js";
13
+ import bernoulliDist from "./random/distributions/bernoulli.js";
14
+ import binomialDist from "./random/distributions/binomial.js";
15
+ import geometricDist from "./random/distributions/geometric.js";
16
+ import poissonDist from "./random/distributions/poisson.js";
17
+ import paretoDist from "./random/distributions/pareto.js";
18
+ import logNormalDist from "./random/distributions/log-normal.js";
19
+ import irwinHallDist from "./random/distributions/irwin-hall.js";
20
+ import batesDist from "./random/distributions/bates.js";
21
+ // Global RNG instance (uses cryptographically secure random by default)
22
+ let globalRNG = new CryptoRNG();
23
+ /**
24
+ * Reset global RNG to cryptographically secure mode.
25
+ * Primarily for testing to avoid test pollution.
26
+ */
27
+ export function resetToCryptoRNG() {
28
+ globalRNG = new CryptoRNG();
29
+ }
30
+ // Platform Function Definitions
31
+ /**
32
+ * Generates a uniform random float in the range [0.0, 1.0).
33
+ *
34
+ * Returns a uniformly distributed random floating-point number greater than or equal to 0.0
35
+ * and strictly less than 1.0. Uses cryptographically secure random number generation by default.
36
+ *
37
+ * This is a platform function for the East language, enabling secure random number generation
38
+ * in East programs running on Node.js.
39
+ *
40
+ * @returns A random float in [0.0, 1.0)
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const generateRandom = East.function([], FloatType, $ => {
45
+ * return Random.uniform();
46
+ * });
47
+ * ```
48
+ *
49
+ * @remarks
50
+ * - Uses Node.js crypto.randomBytes for cryptographic security
51
+ * - Uniform distribution across the range
52
+ * - Never returns exactly 1.0
53
+ */
54
+ export const random_uniform = East.platform("random_uniform", [], FloatType);
55
+ /**
56
+ * Generates a random float from a standard normal distribution (mean=0, standard deviation=1).
57
+ *
58
+ * Returns a random floating-point number drawn from a normal (Gaussian) distribution
59
+ * with mean 0.0 and standard deviation 1.0. Uses the Marsaglia polar method for
60
+ * high-quality normal distribution sampling.
61
+ *
62
+ * This is a platform function for the East language, enabling normal distribution sampling
63
+ * in East programs running on Node.js.
64
+ *
65
+ * @returns A random float from N(0, 1)
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * const generateNormal = East.function([], FloatType, $ => {
70
+ * const z = $.let(Random.normal());
71
+ * // Scale and shift: z * stddev + mean
72
+ * return z.multiply(2.5).add(10.0); // N(10, 2.5)
73
+ * });
74
+ * ```
75
+ *
76
+ * @remarks
77
+ * - Uses Marsaglia polar method for high-quality normal distribution
78
+ * - Mean: 0.0, Standard deviation: 1.0
79
+ * - Can return any real number (unbounded range)
80
+ * - Use `.multiply(stddev).add(mean)` to scale/shift distribution
81
+ */
82
+ export const random_normal = East.platform("random_normal", [], FloatType);
83
+ /**
84
+ * Generates a random integer in the range [min, max] (both endpoints inclusive).
85
+ *
86
+ * Returns a uniformly distributed random integer between min and max (both inclusive).
87
+ * Uses cryptographically secure random number generation. The range must be valid (min ≤ max).
88
+ *
89
+ * This is a platform function for the East language, enabling random integer generation
90
+ * in East programs running on Node.js.
91
+ *
92
+ * @param min - The minimum value (inclusive)
93
+ * @param max - The maximum value (inclusive)
94
+ * @returns A random integer in [min, max]
95
+ *
96
+ * @throws {EastError} When min > max
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * const rollDice = East.function([], IntegerType, $ => {
101
+ * return Random.range(1n, 6n); // 1-6 inclusive
102
+ * });
103
+ * ```
104
+ *
105
+ * @remarks
106
+ * - Both min and max are inclusive
107
+ * - Uniform distribution across the range
108
+ * - Uses floor method to avoid modulo bias
109
+ */
110
+ export const random_range = East.platform("random_range", [IntegerType, IntegerType], IntegerType);
111
+ /**
112
+ * Generates a random float from an exponential distribution.
113
+ *
114
+ * Returns a random floating-point number drawn from an exponential distribution
115
+ * with rate parameter lambda (λ). The mean of the distribution is 1/λ.
116
+ * Commonly used for modeling time between events in a Poisson process.
117
+ *
118
+ * This is a platform function for the East language, enabling exponential distribution sampling
119
+ * in East programs running on Node.js.
120
+ *
121
+ * @param lambda - Rate parameter (λ > 0), default 1.0. Mean = 1/λ
122
+ * @returns A random float from Exp(λ)
123
+ *
124
+ * @throws {EastError} When lambda ≤ 0
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * const waitTime = East.function([], FloatType, $ => {
129
+ * // Average wait time of 5 minutes (lambda = 0.2)
130
+ * return Random.exponential(0.2);
131
+ * });
132
+ * ```
133
+ *
134
+ * @remarks
135
+ * - Uses inverse transform sampling: -ln(U) / λ
136
+ * - Always returns positive values
137
+ * - Mean = 1/λ, Variance = 1/λ²
138
+ * - Memoryless property: P(X > s + t | X > s) = P(X > t)
139
+ */
140
+ export const random_exponential = East.platform("random_exponential", [FloatType], FloatType);
141
+ /**
142
+ * Generates a random float from a Weibull distribution.
143
+ *
144
+ * Returns a random floating-point number drawn from a Weibull distribution
145
+ * with shape parameter k. Uses exponential distribution transformation.
146
+ * Commonly used for modeling failure rates and lifetime distributions.
147
+ *
148
+ * This is a platform function for the East language, enabling Weibull distribution sampling
149
+ * in East programs running on Node.js.
150
+ *
151
+ * @param shape - Shape parameter (k > 0). k=1 gives exponential, k>1 increases failure rate
152
+ * @returns A random float from Weibull(k, 1)
153
+ *
154
+ * @throws {EastError} When shape ≤ 0
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * const lifetime = East.function([], FloatType, $ => {
159
+ * // Shape=2 models wear-out failures
160
+ * return Random.weibull(2.0);
161
+ * });
162
+ * ```
163
+ *
164
+ * @remarks
165
+ * - Uses transformation: exponential() ^ (1/shape)
166
+ * - Shape < 1: decreasing failure rate
167
+ * - Shape = 1: constant failure rate (exponential)
168
+ * - Shape > 1: increasing failure rate
169
+ */
170
+ export const random_weibull = East.platform("random_weibull", [FloatType], FloatType);
171
+ /**
172
+ * Generates a binary random outcome (0 or 1).
173
+ *
174
+ * Returns 0 or 1 based on success probability p. This models a single
175
+ * Bernoulli trial, like a biased coin flip.
176
+ *
177
+ * This is a platform function for the East language, enabling binary random outcomes
178
+ * in East programs running on Node.js.
179
+ *
180
+ * @param p - Success probability (0 ≤ p < 1), default 0.5
181
+ * @returns 0 or 1 (as integer)
182
+ *
183
+ * @throws {EastError} When p < 0 or p ≥ 1
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * const coinFlip = East.function([], IntegerType, $ => {
188
+ * // Fair coin: 50% chance of 1
189
+ * return Random.bernoulli(0.5);
190
+ * });
191
+ * ```
192
+ *
193
+ * @remarks
194
+ * - Returns 1 with probability p, 0 with probability (1-p)
195
+ * - Mean = p, Variance = p(1-p)
196
+ * - Special case of binomial with n=1
197
+ */
198
+ export const random_bernoulli = East.platform("random_bernoulli", [FloatType], IntegerType);
199
+ /**
200
+ * Generates the number of successes in n independent Bernoulli trials.
201
+ *
202
+ * Returns an integer count of successes when performing n independent trials,
203
+ * each with success probability p. Models scenarios like coin flips, pass/fail tests.
204
+ *
205
+ * This is a platform function for the East language, enabling binomial distribution sampling
206
+ * in East programs running on Node.js.
207
+ *
208
+ * @param n - Number of trials (integer > 0)
209
+ * @param p - Success probability per trial (0 ≤ p < 1), default 0.5
210
+ * @returns Number of successes (integer in [0, n])
211
+ *
212
+ * @throws {EastError} When n ≤ 0, not an integer, or p < 0 or p ≥ 1
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * const heads = East.function([], IntegerType, $ => {
217
+ * // Number of heads in 10 coin flips
218
+ * return Random.binomial(10n, 0.5);
219
+ * });
220
+ * ```
221
+ *
222
+ * @remarks
223
+ * - Mean = n × p, Variance = n × p × (1-p)
224
+ * - Returns value in range [0, n]
225
+ * - Sum of n independent Bernoulli(p) trials
226
+ */
227
+ export const random_binomial = East.platform("random_binomial", [IntegerType, FloatType], IntegerType);
228
+ /**
229
+ * Generates the number of trials until the first success.
230
+ *
231
+ * Returns an integer representing the number of independent Bernoulli trials
232
+ * needed to get the first success, where each trial has success probability p.
233
+ * Models scenarios like "how many attempts until success?"
234
+ *
235
+ * This is a platform function for the East language, enabling geometric distribution sampling
236
+ * in East programs running on Node.js.
237
+ *
238
+ * @param p - Success probability per trial (0 < p < 1), default 0.5
239
+ * @returns Number of trials until first success (integer ≥ 1)
240
+ *
241
+ * @throws {EastError} When p ≤ 0 or p ≥ 1
242
+ *
243
+ * @example
244
+ * ```ts
245
+ * const attempts = East.function([], IntegerType, $ => {
246
+ * // Rolls until getting a 6 (p = 1/6)
247
+ * return Random.geometric(1.0 / 6.0);
248
+ * });
249
+ * ```
250
+ *
251
+ * @remarks
252
+ * - Mean = 1/p, Variance = (1-p)/p²
253
+ * - Always returns integer ≥ 1
254
+ * - Memoryless property like exponential distribution
255
+ * - Uses inverse transform sampling
256
+ */
257
+ export const random_geometric = East.platform("random_geometric", [FloatType], IntegerType);
258
+ /**
259
+ * Generates the number of events in a fixed interval from a Poisson process.
260
+ *
261
+ * Returns an integer count of events occurring in a fixed interval,
262
+ * where events occur independently at a constant average rate λ (lambda).
263
+ * Commonly used for modeling rare events, arrivals, or counts.
264
+ *
265
+ * This is a platform function for the East language, enabling Poisson distribution sampling
266
+ * in East programs running on Node.js.
267
+ *
268
+ * @param lambda - Average rate of events (λ > 0), default 1.0
269
+ * @returns Number of events (integer ≥ 0)
270
+ *
271
+ * @throws {EastError} When lambda ≤ 0
272
+ *
273
+ * @example
274
+ * ```ts
275
+ * const customers = East.function([], IntegerType, $ => {
276
+ * // Average 3 customers per hour
277
+ * return Random.poisson(3.0);
278
+ * });
279
+ * ```
280
+ *
281
+ * @remarks
282
+ * - Mean = λ, Variance = λ
283
+ * - Always returns non-negative integer
284
+ * - Uses inversion method for λ < 10, generative method for λ ≥ 10
285
+ * - Approximates binomial when n is large and p is small
286
+ */
287
+ export const random_poisson = East.platform("random_poisson", [FloatType], IntegerType);
288
+ /**
289
+ * Generates a random float from a Pareto distribution (power law).
290
+ *
291
+ * Returns a random floating-point number drawn from a Pareto distribution
292
+ * with shape parameter α (alpha). Models phenomena following power laws,
293
+ * like wealth distribution, city sizes, or word frequencies.
294
+ *
295
+ * This is a platform function for the East language, enabling Pareto distribution sampling
296
+ * in East programs running on Node.js.
297
+ *
298
+ * @param alpha - Shape parameter (α ≥ 0), default 1.0. Smaller α = heavier tail
299
+ * @returns A random float from Pareto(α) (value ≥ 1.0)
300
+ *
301
+ * @throws {EastError} When alpha < 0
302
+ *
303
+ * @example
304
+ * ```ts
305
+ * const wealth = East.function([], FloatType, $ => {
306
+ * // Heavy-tailed distribution (80/20 rule)
307
+ * return Random.pareto(1.16);
308
+ * });
309
+ * ```
310
+ *
311
+ * @remarks
312
+ * - Always returns value ≥ 1.0
313
+ * - Mean = α/(α-1) for α > 1, infinite otherwise
314
+ * - Heavier tail as α decreases
315
+ * - Models "80/20 rule" and power law phenomena
316
+ */
317
+ export const random_pareto = East.platform("random_pareto", [FloatType], FloatType);
318
+ /**
319
+ * Generates a random float whose logarithm is normally distributed.
320
+ *
321
+ * Returns a random floating-point number from a log-normal distribution,
322
+ * where ln(X) ~ N(μ, σ²). Commonly used for modeling multiplicative processes,
323
+ * stock prices, or quantities that can't be negative.
324
+ *
325
+ * This is a platform function for the East language, enabling log-normal distribution sampling
326
+ * in East programs running on Node.js.
327
+ *
328
+ * @param mu - Mean of underlying normal distribution, default 0.0
329
+ * @param sigma - Standard deviation of underlying normal, default 1.0
330
+ * @returns A random float from LogNormal(μ, σ) (always positive)
331
+ *
332
+ * @example
333
+ * ```ts
334
+ * const stockPrice = East.function([], FloatType, $ => {
335
+ * // Log-normal with ln(X) ~ N(0, 0.2)
336
+ * return Random.logNormal(0.0, 0.2);
337
+ * });
338
+ * ```
339
+ *
340
+ * @remarks
341
+ * - Always returns positive values
342
+ * - If ln(X) ~ N(μ, σ²), then X ~ LogNormal(μ, σ)
343
+ * - Median = exp(μ), Mean = exp(μ + σ²/2)
344
+ * - Right-skewed distribution
345
+ */
346
+ export const random_log_normal = East.platform("random_log_normal", [FloatType, FloatType], FloatType);
347
+ /**
348
+ * Generates the sum of n independent uniform random variables.
349
+ *
350
+ * Returns the sum of n independent uniform(0,1) random variables.
351
+ * This is the Irwin-Hall distribution. As n increases, approaches
352
+ * normal distribution by Central Limit Theorem.
353
+ *
354
+ * This is a platform function for the East language, enabling Irwin-Hall distribution sampling
355
+ * in East programs running on Node.js.
356
+ *
357
+ * @param n - Number of uniform variables to sum (integer ≥ 0), default 1
358
+ * @returns Sum of n uniform(0,1) random variables (float in [0, n])
359
+ *
360
+ * @throws {EastError} When n < 0 or not an integer
361
+ *
362
+ * @example
363
+ * ```ts
364
+ * const irwinHall = East.function([], FloatType, $ => {
365
+ * // Sum of 12 uniform variables
366
+ * return Random.irwinHall(12n);
367
+ * });
368
+ * ```
369
+ *
370
+ * @remarks
371
+ * - Range: [0, n]
372
+ * - Mean = n/2, Variance = n/12
373
+ * - Approximates N(n/2, n/12) for large n
374
+ * - Related to Bates distribution (divided by n)
375
+ */
376
+ export const random_irwin_hall = East.platform("random_irwin_hall", [IntegerType], FloatType);
377
+ /**
378
+ * Generates the average of n independent uniform random variables.
379
+ *
380
+ * Returns the average of n independent uniform(0,1) random variables.
381
+ * This is the Bates distribution. Approaches normal distribution as
382
+ * n increases (Central Limit Theorem).
383
+ *
384
+ * This is a platform function for the East language, enabling Bates distribution sampling
385
+ * in East programs running on Node.js.
386
+ *
387
+ * @param n - Number of uniform variables to average (integer > 0), default 1
388
+ * @returns Average of n uniform(0,1) random variables (float in [0, 1])
389
+ *
390
+ * @throws {EastError} When n ≤ 0 or not an integer
391
+ *
392
+ * @example
393
+ * ```ts
394
+ * const bates = East.function([], FloatType, $ => {
395
+ * // Average of 12 uniform variables (smoother than single uniform)
396
+ * return Random.bates(12n);
397
+ * });
398
+ * ```
399
+ *
400
+ * @remarks
401
+ * - Range: [0, 1]
402
+ * - Mean = 0.5, Variance = 1/(12n)
403
+ * - Approximates N(0.5, 1/(12n)) for large n
404
+ * - More concentrated around 0.5 as n increases
405
+ */
406
+ export const random_bates = East.platform("random_bates", [IntegerType], FloatType);
407
+ /**
408
+ * Seeds the global random number generator.
409
+ *
410
+ * Sets the seed for the random number generator, allowing reproducible random sequences.
411
+ * All subsequent random operations will use this seeded RNG until a new seed is set
412
+ * or the process restarts.
413
+ *
414
+ * This is a platform function for the East language, enabling reproducible randomness
415
+ * in East programs running on Node.js.
416
+ *
417
+ * @param seed - Seed value (integer)
418
+ * @returns null
419
+ *
420
+ * @example
421
+ * ```ts
422
+ * const seedAndGenerate = East.function([], FloatType, $ => {
423
+ * $(Random.seed(12345n));
424
+ * return Random.uniform(); // Will be reproducible
425
+ * });
426
+ * ```
427
+ *
428
+ * @remarks
429
+ * - IMPORTANT: Seeding switches from cryptographically secure random to a deterministic
430
+ * PRNG (xorshift128+). This is essential for reproducible simulations but should not
431
+ * be used when cryptographic security is required.
432
+ * - Same seed produces same sequence of random numbers
433
+ * - Useful for testing and reproducible simulations
434
+ */
435
+ export const random_seed = East.platform("random_seed", [IntegerType], NullType);
436
+ // Platform Function Implementations
437
+ const RandomImpl = [
438
+ random_uniform.implement(() => {
439
+ return uniformDist(globalRNG, 0, 1)();
440
+ }),
441
+ random_normal.implement(() => {
442
+ return normalDist(globalRNG, 0, 1)();
443
+ }),
444
+ random_range.implement((min, max) => {
445
+ if (min > max) {
446
+ throw new EastError(`Invalid range: min (${min}) > max (${max})`, {
447
+ location: { filename: "random_range", line: 0n, column: 0n },
448
+ });
449
+ }
450
+ try {
451
+ return BigInt(uniformIntDist(globalRNG, min, max)());
452
+ }
453
+ catch (err) {
454
+ throw new EastError(`Random range generation failed: ${err.message}`, {
455
+ location: { filename: "random_range", line: 0n, column: 0n },
456
+ cause: err
457
+ });
458
+ }
459
+ }),
460
+ random_exponential.implement((lambda) => {
461
+ try {
462
+ return exponentialDist(globalRNG, lambda)();
463
+ }
464
+ catch (err) {
465
+ throw new EastError(`Random exponential generation failed: ${err.message}`, {
466
+ location: { filename: "random_exponential", line: 0n, column: 0n },
467
+ cause: err
468
+ });
469
+ }
470
+ }),
471
+ random_weibull.implement((shape) => {
472
+ try {
473
+ // Weibull = exponential() ^ (1/shape)
474
+ const exp = exponentialDist(globalRNG, 1);
475
+ return Math.pow(exp(), 1 / shape);
476
+ }
477
+ catch (err) {
478
+ throw new EastError(`Random Weibull generation failed: ${err.message}`, {
479
+ location: { filename: "random_weibull", line: 0n, column: 0n },
480
+ cause: err
481
+ });
482
+ }
483
+ }),
484
+ random_bernoulli.implement((p) => {
485
+ try {
486
+ return BigInt(bernoulliDist(globalRNG, p)());
487
+ }
488
+ catch (err) {
489
+ throw new EastError(`Random Bernoulli generation failed: ${err.message}`, {
490
+ location: { filename: "random_bernoulli", line: 0n, column: 0n },
491
+ cause: err
492
+ });
493
+ }
494
+ }),
495
+ random_binomial.implement((n, p) => {
496
+ try {
497
+ return BigInt(binomialDist(globalRNG, n, p)());
498
+ }
499
+ catch (err) {
500
+ throw new EastError(`Random binomial generation failed: ${err.message}`, {
501
+ location: { filename: "random_binomial", line: 0n, column: 0n },
502
+ cause: err
503
+ });
504
+ }
505
+ }),
506
+ random_geometric.implement((p) => {
507
+ try {
508
+ return BigInt(geometricDist(globalRNG, p)());
509
+ }
510
+ catch (err) {
511
+ throw new EastError(`Random geometric generation failed: ${err.message}`, {
512
+ location: { filename: "random_geometric", line: 0n, column: 0n },
513
+ cause: err
514
+ });
515
+ }
516
+ }),
517
+ random_poisson.implement((lambda) => {
518
+ try {
519
+ return BigInt(poissonDist(globalRNG, lambda)());
520
+ }
521
+ catch (err) {
522
+ throw new EastError(`Random Poisson generation failed: ${err.message}`, {
523
+ location: { filename: "random_poisson", line: 0n, column: 0n },
524
+ cause: err
525
+ });
526
+ }
527
+ }),
528
+ random_pareto.implement((alpha) => {
529
+ try {
530
+ return paretoDist(globalRNG, alpha)();
531
+ }
532
+ catch (err) {
533
+ throw new EastError(`Random Pareto generation failed: ${err.message}`, {
534
+ location: { filename: "random_pareto", line: 0n, column: 0n },
535
+ cause: err
536
+ });
537
+ }
538
+ }),
539
+ random_log_normal.implement((mu, sigma) => {
540
+ try {
541
+ return logNormalDist(globalRNG, mu, sigma)();
542
+ }
543
+ catch (err) {
544
+ throw new EastError(`Random log-normal generation failed: ${err.message}`, {
545
+ location: { filename: "random_log_normal", line: 0n, column: 0n },
546
+ cause: err
547
+ });
548
+ }
549
+ }),
550
+ random_irwin_hall.implement((n) => {
551
+ try {
552
+ return irwinHallDist(globalRNG, n)();
553
+ }
554
+ catch (err) {
555
+ throw new EastError(`Random Irwin-Hall generation failed: ${err.message}`, {
556
+ location: { filename: "random_irwin_hall", line: 0n, column: 0n },
557
+ cause: err
558
+ });
559
+ }
560
+ }),
561
+ random_bates.implement((n) => {
562
+ try {
563
+ return batesDist(globalRNG, n)();
564
+ }
565
+ catch (err) {
566
+ throw new EastError(`Random Bates generation failed: ${err.message}`, {
567
+ location: { filename: "random_bates", line: 0n, column: 0n },
568
+ cause: err
569
+ });
570
+ }
571
+ }),
572
+ random_seed.implement((seed) => {
573
+ // Switch to seedable PRNG (xorshift128+) and set the seed
574
+ // IMPORTANT: This disables cryptographic security for reproducible simulations
575
+ globalRNG = new XorShift128RNG(seed);
576
+ }),
577
+ ];
578
+ /**
579
+ * Grouped random platform functions.
580
+ *
581
+ * Provides random number generation and sampling operations for East programs,
582
+ * including multiple probability distributions and collection sampling.
583
+ *
584
+ * @example
585
+ * ```ts
586
+ * import { East, FloatType, IntegerType } from "@elaraai/east";
587
+ * import { Random } from "@elaraai/east-node-std";
588
+ *
589
+ * const rollDice = East.function([], IntegerType, $ => {
590
+ * return Random.range(1n, 6n);
591
+ * });
592
+ *
593
+ * const compiled = East.compile(rollDice.toIR(), Random.Implementation);
594
+ * const result = compiled(); // Random integer from 1 to 6
595
+ * ```
596
+ */
597
+ export const Random = {
598
+ /**
599
+ * Generates a uniform random float in the range [0.0, 1.0).
600
+ *
601
+ * Returns a uniformly distributed random floating-point number greater than or equal to 0.0
602
+ * and strictly less than 1.0. Uses cryptographically secure random number generation.
603
+ *
604
+ * @returns A random float in [0.0, 1.0)
605
+ *
606
+ * @example
607
+ * ```ts
608
+ * const generateRandom = East.function([], FloatType, $ => {
609
+ * return Random.uniform();
610
+ * });
611
+ *
612
+ * const compiled = East.compile(generateRandom.toIR(), Random.Implementation);
613
+ * const result = compiled(); // e.g., 0.7234891234
614
+ * ```
615
+ */
616
+ uniform: random_uniform,
617
+ /**
618
+ * Generates a random float from a standard normal distribution (mean=0, standard deviation=1).
619
+ *
620
+ * Returns a random floating-point number drawn from a normal (Gaussian) distribution
621
+ * with mean 0.0 and standard deviation 1.0. Use multiplication and addition to scale
622
+ * and shift to different mean/standard deviation.
623
+ *
624
+ * @returns A random float from N(0, 1)
625
+ *
626
+ * @example
627
+ * ```ts
628
+ * const generateNormal = East.function([], FloatType, $ => {
629
+ * const z = $.let(Random.normal());
630
+ * // Scale and shift: z * stddev + mean
631
+ * return z.multiply(2.5).add(10.0); // N(10, 2.5)
632
+ * });
633
+ *
634
+ * const compiled = East.compile(generateNormal.toIR(), Random.Implementation);
635
+ * const result = compiled(); // e.g., 9.234 (from N(10, 2.5))
636
+ * ```
637
+ */
638
+ normal: random_normal,
639
+ /**
640
+ * Generates a random integer in the range [min, max] (both endpoints inclusive).
641
+ *
642
+ * Returns a uniformly distributed random integer between min and max (both inclusive).
643
+ * Uses cryptographically secure random number generation.
644
+ *
645
+ * @param min - The minimum value (inclusive)
646
+ * @param max - The maximum value (inclusive, must be >= min)
647
+ * @returns A random integer in [min, max]
648
+ *
649
+ * @throws {EastError} When min > max
650
+ *
651
+ * @example
652
+ * ```ts
653
+ * const rollDice = East.function([], IntegerType, $ => {
654
+ * return Random.range(1n, 6n); // 1-6 inclusive
655
+ * });
656
+ *
657
+ * const compiled = East.compile(rollDice.toIR(), Random.Implementation);
658
+ * const result = compiled(); // e.g., 4n
659
+ * ```
660
+ */
661
+ range: random_range,
662
+ /**
663
+ * Generates a random float from an exponential distribution.
664
+ *
665
+ * Returns a random floating-point number drawn from an exponential distribution
666
+ * with rate parameter lambda (λ). The mean of the distribution is 1/λ.
667
+ * Commonly used for modeling time between events in a Poisson process.
668
+ *
669
+ * @param lambda - Rate parameter (λ > 0), where mean = 1/λ
670
+ * @returns A random float from Exp(λ)
671
+ *
672
+ * @throws {EastError} When lambda <= 0
673
+ *
674
+ * @example
675
+ * ```ts
676
+ * const waitTime = East.function([], FloatType, $ => {
677
+ * // Average wait time of 5 minutes (lambda = 0.2)
678
+ * return Random.exponential(0.2);
679
+ * });
680
+ *
681
+ * const compiled = East.compile(waitTime.toIR(), Random.Implementation);
682
+ * const result = compiled(); // e.g., 3.7 minutes
683
+ * ```
684
+ */
685
+ exponential: random_exponential,
686
+ /**
687
+ * Generates a random float from a Weibull distribution.
688
+ *
689
+ * Returns a random floating-point number drawn from a Weibull distribution
690
+ * with shape parameter k. Commonly used for modeling failure rates and lifetime distributions.
691
+ *
692
+ * @param shape - Shape parameter (k > 0). k=1 gives exponential, k>1 increases failure rate
693
+ * @returns A random float from Weibull(k, 1)
694
+ *
695
+ * @throws {EastError} When shape <= 0
696
+ *
697
+ * @example
698
+ * ```ts
699
+ * const lifetime = East.function([], FloatType, $ => {
700
+ * // Shape=2 models wear-out failures
701
+ * return Random.weibull(2.0);
702
+ * });
703
+ *
704
+ * const compiled = East.compile(lifetime.toIR(), Random.Implementation);
705
+ * const result = compiled(); // e.g., 1.23
706
+ * ```
707
+ */
708
+ weibull: random_weibull,
709
+ /**
710
+ * Generates a binary random outcome (0 or 1).
711
+ *
712
+ * Returns 0 or 1 based on success probability p. This models a single
713
+ * Bernoulli trial, like a biased coin flip.
714
+ *
715
+ * @param p - Success probability (0 <= p < 1)
716
+ * @returns 0 or 1 (as integer)
717
+ *
718
+ * @throws {EastError} When p < 0 or p >= 1
719
+ *
720
+ * @example
721
+ * ```ts
722
+ * const coinFlip = East.function([], IntegerType, $ => {
723
+ * // Fair coin: 50% chance of 1
724
+ * return Random.bernoulli(0.5);
725
+ * });
726
+ *
727
+ * const compiled = East.compile(coinFlip.toIR(), Random.Implementation);
728
+ * const result = compiled(); // e.g., 1n or 0n
729
+ * ```
730
+ */
731
+ bernoulli: random_bernoulli,
732
+ /**
733
+ * Generates the number of successes in n independent Bernoulli trials.
734
+ *
735
+ * Returns an integer count of successes when performing n independent trials,
736
+ * each with success probability p. Models scenarios like coin flips, pass/fail tests.
737
+ *
738
+ * @param n - Number of trials (integer > 0)
739
+ * @param p - Success probability per trial (0 <= p < 1)
740
+ * @returns Number of successes (integer in [0, n])
741
+ *
742
+ * @throws {EastError} When n <= 0, not an integer, or p < 0 or p >= 1
743
+ *
744
+ * @example
745
+ * ```ts
746
+ * const heads = East.function([], IntegerType, $ => {
747
+ * // Number of heads in 10 coin flips
748
+ * return Random.binomial(10n, 0.5);
749
+ * });
750
+ *
751
+ * const compiled = East.compile(heads.toIR(), Random.Implementation);
752
+ * const result = compiled(); // e.g., 6n
753
+ * ```
754
+ */
755
+ binomial: random_binomial,
756
+ /**
757
+ * Generates the number of trials until the first success.
758
+ *
759
+ * Returns an integer representing the number of independent Bernoulli trials
760
+ * needed to get the first success, where each trial has success probability p.
761
+ * Models scenarios like "how many attempts until success?"
762
+ *
763
+ * @param p - Success probability per trial (0 < p < 1)
764
+ * @returns Number of trials until first success (integer >= 1)
765
+ *
766
+ * @throws {EastError} When p <= 0 or p >= 1
767
+ *
768
+ * @example
769
+ * ```ts
770
+ * const attempts = East.function([], IntegerType, $ => {
771
+ * // Rolls until getting a 6 (p = 1/6)
772
+ * return Random.geometric(1.0 / 6.0);
773
+ * });
774
+ *
775
+ * const compiled = East.compile(attempts.toIR(), Random.Implementation);
776
+ * const result = compiled(); // e.g., 4n
777
+ * ```
778
+ */
779
+ geometric: random_geometric,
780
+ /**
781
+ * Generates the number of events in a fixed interval from a Poisson process.
782
+ *
783
+ * Returns an integer count of events occurring in a fixed interval,
784
+ * where events occur independently at a constant average rate λ (lambda).
785
+ * Commonly used for modeling rare events, arrivals, or counts.
786
+ *
787
+ * @param lambda - Average rate of events (λ > 0)
788
+ * @returns Number of events (integer >= 0)
789
+ *
790
+ * @throws {EastError} When lambda <= 0
791
+ *
792
+ * @example
793
+ * ```ts
794
+ * const customers = East.function([], IntegerType, $ => {
795
+ * // Average 3 customers per hour
796
+ * return Random.poisson(3.0);
797
+ * });
798
+ *
799
+ * const compiled = East.compile(customers.toIR(), Random.Implementation);
800
+ * const result = compiled(); // e.g., 2n
801
+ * ```
802
+ */
803
+ poisson: random_poisson,
804
+ /**
805
+ * Generates a random float from a Pareto distribution (power law).
806
+ *
807
+ * Returns a random floating-point number drawn from a Pareto distribution
808
+ * with shape parameter α (alpha). Models phenomena following power laws,
809
+ * like wealth distribution, city sizes, or word frequencies.
810
+ *
811
+ * @param alpha - Shape parameter (α >= 0). Smaller α = heavier tail
812
+ * @returns A random float from Pareto(α) (value >= 1.0)
813
+ *
814
+ * @throws {EastError} When alpha < 0
815
+ *
816
+ * @example
817
+ * ```ts
818
+ * const wealth = East.function([], FloatType, $ => {
819
+ * // Heavy-tailed distribution (80/20 rule)
820
+ * return Random.pareto(1.16);
821
+ * });
822
+ *
823
+ * const compiled = East.compile(wealth.toIR(), Random.Implementation);
824
+ * const result = compiled(); // e.g., 2.45
825
+ * ```
826
+ */
827
+ pareto: random_pareto,
828
+ /**
829
+ * Generates a random float whose logarithm is normally distributed.
830
+ *
831
+ * Returns a random floating-point number from a log-normal distribution,
832
+ * where ln(X) ~ N(μ, σ²). Commonly used for modeling multiplicative processes,
833
+ * stock prices, or quantities that can't be negative.
834
+ *
835
+ * @param mu - Mean of underlying normal distribution
836
+ * @param sigma - Standard deviation of underlying normal (must be > 0)
837
+ * @returns A random float from LogNormal(μ, σ) (always positive)
838
+ *
839
+ * @example
840
+ * ```ts
841
+ * const stockPrice = East.function([], FloatType, $ => {
842
+ * // Log-normal with ln(X) ~ N(0, 0.2)
843
+ * return Random.logNormal(0.0, 0.2);
844
+ * });
845
+ *
846
+ * const compiled = East.compile(stockPrice.toIR(), Random.Implementation);
847
+ * const result = compiled(); // e.g., 1.15
848
+ * ```
849
+ */
850
+ logNormal: random_log_normal,
851
+ /**
852
+ * Generates the sum of n independent uniform random variables.
853
+ *
854
+ * Returns the sum of n independent uniform(0,1) random variables.
855
+ * This is the Irwin-Hall distribution. As n increases, approaches
856
+ * normal distribution by Central Limit Theorem.
857
+ *
858
+ * @param n - Number of uniform variables to sum (integer >= 0)
859
+ * @returns Sum of n uniform(0,1) random variables (float in [0, n])
860
+ *
861
+ * @throws {EastError} When n < 0 or not an integer
862
+ *
863
+ * @example
864
+ * ```ts
865
+ * const irwinHall = East.function([], FloatType, $ => {
866
+ * // Sum of 12 uniform variables
867
+ * return Random.irwinHall(12n);
868
+ * });
869
+ *
870
+ * const compiled = East.compile(irwinHall.toIR(), Random.Implementation);
871
+ * const result = compiled(); // e.g., 6.234 (approximately N(6, 1))
872
+ * ```
873
+ */
874
+ irwinHall: random_irwin_hall,
875
+ /**
876
+ * Generates the average of n independent uniform random variables.
877
+ *
878
+ * Returns the average of n independent uniform(0,1) random variables.
879
+ * This is the Bates distribution. Approaches normal distribution as
880
+ * n increases (Central Limit Theorem).
881
+ *
882
+ * @param n - Number of uniform variables to average (integer > 0)
883
+ * @returns Average of n uniform(0,1) random variables (float in [0, 1])
884
+ *
885
+ * @throws {EastError} When n <= 0 or not an integer
886
+ *
887
+ * @example
888
+ * ```ts
889
+ * const bates = East.function([], FloatType, $ => {
890
+ * // Average of 12 uniform variables (smoother than single uniform)
891
+ * return Random.bates(12n);
892
+ * });
893
+ *
894
+ * const compiled = East.compile(bates.toIR(), Random.Implementation);
895
+ * const result = compiled(); // e.g., 0.523 (approximately N(0.5, 1/144))
896
+ * ```
897
+ */
898
+ bates: random_bates,
899
+ /**
900
+ * Seeds the global random number generator.
901
+ *
902
+ * Sets the seed for the random number generator, allowing reproducible random sequences.
903
+ * All subsequent random operations will use this seeded RNG until a new seed is set
904
+ * or the process restarts.
905
+ *
906
+ * @param seed - Seed value (integer)
907
+ * @returns null
908
+ *
909
+ * @example
910
+ * ```ts
911
+ * const seedAndGenerate = East.function([], FloatType, $ => {
912
+ * $(Random.seed(12345n));
913
+ * return Random.uniform(); // Will be reproducible
914
+ * });
915
+ *
916
+ * const compiled = East.compile(seedAndGenerate.toIR(), Random.Implementation);
917
+ * const result = compiled(); // Same result every time with same seed
918
+ * ```
919
+ *
920
+ * @remarks
921
+ * IMPORTANT: Seeding switches from cryptographically secure random to a deterministic
922
+ * PRNG (xorshift128+). Essential for reproducible simulations.
923
+ */
924
+ seed: random_seed,
925
+ /**
926
+ * Node.js implementation of random platform functions.
927
+ *
928
+ * Pass this to {@link East.compile} to enable random operations.
929
+ */
930
+ Implementation: RandomImpl,
931
+ };
932
+ // Export RandomImpl for backwards compatibility
933
+ export { RandomImpl };
934
+ //# sourceMappingURL=random.js.map