@cloudglides/nox 1.1.1 → 1.1.2

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": "@cloudglides/nox",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Unpredictable random number generator with multiple algorithms and distributions",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,10 @@
1
+ export { rng, RNG } from './rng.browser.js';
2
+ export { deterministic } from './presets.js';
3
+
4
+ export { normal, exponential, uniform, poisson } from './utils/distributions.js';
5
+ export { shuffle, pick, sample } from './utils/sequence.js';
6
+ export { saveState, restoreState, cloneGenerator } from './utils/state.js';
7
+ export { weightedPick, weightedSample, reservoirSample } from './utils/sampling.js';
8
+ export { meanTest, varianceTest, kolmogorovSmirnovTest } from './utils/statistics.js';
9
+
10
+ export { combined, clearCryptoCache } from './utils/entropy.browser.js';
@@ -18,7 +18,7 @@ export {
18
18
  meanTest,
19
19
  varianceTest,
20
20
  kolmogorovSmirnovTest
21
- } from './core.js';
21
+ } from './core.browser.js';
22
22
 
23
23
  export {
24
24
  Xorshift64,
@@ -0,0 +1,141 @@
1
+ import { PCG64 } from './generators/index.js';
2
+ import { combined } from './utils/entropy.browser.js';
3
+
4
+ export class RNG {
5
+ constructor(generator = null, seed = null) {
6
+ if (generator === null) {
7
+ seed = seed || combined();
8
+ this.gen = new PCG64(seed);
9
+ } else if (typeof generator === 'function') {
10
+ this.gen = new generator(seed || combined());
11
+ } else if (typeof generator === 'object' && generator !== null) {
12
+ this.gen = generator;
13
+ } else {
14
+ throw new TypeError('Generator must be a constructor function or instance');
15
+ }
16
+ }
17
+
18
+ next() {
19
+ return this.gen.next();
20
+ }
21
+
22
+ nextInt(max = 2147483647) {
23
+ return this.gen.nextInt(max);
24
+ }
25
+
26
+ nextFloat() {
27
+ return this.gen.nextFloat();
28
+ }
29
+
30
+ int(min, max) {
31
+ if (typeof min !== 'number' || typeof max !== 'number') {
32
+ throw new TypeError('int() requires two number arguments');
33
+ }
34
+ if (!Number.isInteger(min) || !Number.isInteger(max)) {
35
+ throw new TypeError('int() arguments must be integers');
36
+ }
37
+ if (min > max) [min, max] = [max, min];
38
+ return Math.floor(this.nextFloat() * (max - min + 1)) + min;
39
+ }
40
+
41
+ bool(probability = 0.5) {
42
+ if (typeof probability !== 'number') {
43
+ throw new TypeError('probability must be a number');
44
+ }
45
+ if (probability < 0 || probability > 1) {
46
+ throw new RangeError('probability must be between 0 and 1');
47
+ }
48
+ return this.nextFloat() < probability;
49
+ }
50
+
51
+ range(min, max, step = 1) {
52
+ if (typeof min !== 'number' || typeof max !== 'number' || typeof step !== 'number') {
53
+ throw new TypeError('min, max, and step must be numbers');
54
+ }
55
+ if (!Number.isInteger(min) || !Number.isInteger(max) || !Number.isInteger(step)) {
56
+ throw new Error('min, max, and step must be integers');
57
+ }
58
+ if (step <= 0) {
59
+ throw new Error('step must be positive');
60
+ }
61
+ if (min > max) {
62
+ throw new Error('min must be less than or equal to max');
63
+ }
64
+
65
+ const count = Math.floor((max - min) / step) + 1;
66
+ const idx = this.nextInt(count);
67
+ return min + idx * step;
68
+ }
69
+
70
+ choice(arr) {
71
+ if (!Array.isArray(arr) || arr.length === 0) {
72
+ throw new TypeError('choice() requires non-empty array');
73
+ }
74
+ return arr[this.nextInt(arr.length)];
75
+ }
76
+
77
+ batch(count, fn) {
78
+ if (typeof count !== 'number' || !Number.isInteger(count)) {
79
+ throw new TypeError('count must be an integer');
80
+ }
81
+ if (count < 0) {
82
+ throw new RangeError('count must be non-negative');
83
+ }
84
+ if (typeof fn !== 'function') {
85
+ throw new TypeError('fn must be a function');
86
+ }
87
+
88
+ const result = [];
89
+ for (let i = 0; i < count; i++) {
90
+ result.push(fn(this, i));
91
+ }
92
+ return result;
93
+ }
94
+
95
+ floats(count) {
96
+ if (typeof count !== 'number' || !Number.isInteger(count)) {
97
+ throw new TypeError('count must be an integer');
98
+ }
99
+ if (count < 0) {
100
+ throw new RangeError('count must be non-negative');
101
+ }
102
+
103
+ const result = new Array(count);
104
+ for (let i = 0; i < count; i++) {
105
+ result[i] = this.nextFloat();
106
+ }
107
+ return result;
108
+ }
109
+
110
+ ints(count, max = 2147483647) {
111
+ if (typeof count !== 'number' || !Number.isInteger(count)) {
112
+ throw new TypeError('count must be an integer');
113
+ }
114
+ if (count < 0) {
115
+ throw new RangeError('count must be non-negative');
116
+ }
117
+
118
+ const result = new Array(count);
119
+ for (let i = 0; i < count; i++) {
120
+ result[i] = this.nextInt(max);
121
+ }
122
+ return result;
123
+ }
124
+
125
+ bools(count, probability = 0.5) {
126
+ if (typeof count !== 'number' || !Number.isInteger(count)) {
127
+ throw new TypeError('count must be an integer');
128
+ }
129
+ if (count < 0) {
130
+ throw new RangeError('count must be non-negative');
131
+ }
132
+
133
+ const result = new Array(count);
134
+ for (let i = 0; i < count; i++) {
135
+ result[i] = this.bool(probability);
136
+ }
137
+ return result;
138
+ }
139
+ }
140
+
141
+ export const rng = () => new RNG();
@@ -7,26 +7,22 @@ const isBrowser = typeof window !== 'undefined';
7
7
  let performanceImpl = typeof performance !== 'undefined' ? performance : null;
8
8
  let randomBytesImpl = null;
9
9
 
10
- // Avoid parsing require() for browser - use indirect method
11
- if (isNode) {
10
+ // Only require in true Node environment
11
+ if (isNode && !isBrowser) {
12
12
  try {
13
- const getRequire = new Function('return require');
14
- const req = getRequire();
15
- const perf_hooks = req('perf_hooks');
16
- performanceImpl = perf_hooks.performance;
13
+ // Avoid static analysis by constructing module name dynamically
14
+ const moduleName = ['perf', '_hooks'].join('');
15
+ performanceImpl = require(moduleName).performance;
17
16
  } catch {}
18
17
 
19
18
  try {
20
- const getRequire = new Function('return require');
21
- const req = getRequire();
22
- const crypto_mod = req('crypto');
23
- randomBytesImpl = crypto_mod.randomBytes;
19
+ randomBytesImpl = require('crypto').randomBytes;
24
20
  } catch {}
25
21
  }
26
22
 
27
23
  export const fromPerformance = () => {
28
24
  try {
29
- if (performanceImpl && performanceImpl.now) {
25
+ if (performanceImpl && typeof performanceImpl.now === 'function') {
30
26
  const t = performanceImpl.now();
31
27
  return BigInt(Math.floor(t * 1000)) ^ BigInt(Math.floor(t * 1000000) % 1000000);
32
28
  }
@@ -38,7 +34,7 @@ export const fromPerformance = () => {
38
34
 
39
35
  export const fromMemory = () => {
40
36
  try {
41
- if (isNode && typeof process !== 'undefined' && process.memoryUsage) {
37
+ if (isNode && !isBrowser && typeof process !== 'undefined' && process.memoryUsage) {
42
38
  const mem = process.memoryUsage();
43
39
  const total = mem.heapUsed + mem.external + mem.heapTotal;
44
40
  return BigInt(Math.floor(total)) ^ BigInt(Date.now());
@@ -64,7 +60,7 @@ export const fromCrypto = (bytes = 8) => {
64
60
  for (let i = 0; i < buf.length; i++) {
65
61
  val = (val << 8n) | BigInt(buf[i]);
66
62
  }
67
- } else if (isBrowser && typeof crypto !== 'undefined' && crypto.getRandomValues) {
63
+ } else if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
68
64
  const arr = new Uint8Array(Math.max(bytes, 8));
69
65
  crypto.getRandomValues(arr);
70
66
  for (let i = 0; i < arr.length; i++) {