@cloudglides/nox 1.1.0 → 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.
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "nox-demo",
3
+ "private": true,
4
+ "version": "0.0.1",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "vite build",
9
+ "preview": "vite preview"
10
+ },
11
+ "dependencies": {
12
+ "@cloudglides/nox": "latest",
13
+ "react": "^18.2.0",
14
+ "react-dom": "^18.2.0"
15
+ },
16
+ "devDependencies": {
17
+ "@vitejs/plugin-react": "^4.2.1",
18
+ "vite": "^5.0.8"
19
+ }
20
+ }
@@ -0,0 +1,161 @@
1
+ .app {
2
+ width: 100%;
3
+ min-height: 100vh;
4
+ display: flex;
5
+ flex-direction: column;
6
+ background: linear-gradient(135deg, #1e1e2e 0%, #2d2d44 100%);
7
+ }
8
+
9
+ header {
10
+ padding: 3rem 2rem;
11
+ text-align: center;
12
+ border-bottom: 2px solid #646cff;
13
+ }
14
+
15
+ header h1 {
16
+ font-size: 3rem;
17
+ margin-bottom: 0.5rem;
18
+ background: linear-gradient(45deg, #646cff, #0ea5e9);
19
+ -webkit-background-clip: text;
20
+ -webkit-text-fill-color: transparent;
21
+ background-clip: text;
22
+ }
23
+
24
+ header p {
25
+ font-size: 1.1rem;
26
+ color: #a0a0a0;
27
+ }
28
+
29
+ .tabs {
30
+ display: flex;
31
+ gap: 1rem;
32
+ padding: 2rem;
33
+ justify-content: center;
34
+ flex-wrap: wrap;
35
+ border-bottom: 1px solid #444;
36
+ }
37
+
38
+ .tabs button {
39
+ padding: 0.8rem 1.6rem;
40
+ font-size: 1rem;
41
+ border: 2px solid transparent;
42
+ border-radius: 8px;
43
+ cursor: pointer;
44
+ transition: all 0.3s;
45
+ color: #a0a0a0;
46
+ background: #2d2d44;
47
+ }
48
+
49
+ .tabs button:hover {
50
+ color: #646cff;
51
+ border-color: #646cff;
52
+ }
53
+
54
+ .tabs button.active {
55
+ color: #fff;
56
+ border-color: #646cff;
57
+ background: #1a1a2e;
58
+ }
59
+
60
+ .content {
61
+ flex: 1;
62
+ padding: 2rem;
63
+ max-width: 900px;
64
+ margin: 0 auto;
65
+ width: 100%;
66
+ }
67
+
68
+ .section {
69
+ background: #2d2d44;
70
+ padding: 2rem;
71
+ border-radius: 12px;
72
+ border: 1px solid #444;
73
+ }
74
+
75
+ .section h2 {
76
+ margin-bottom: 1.5rem;
77
+ color: #646cff;
78
+ }
79
+
80
+ .action-btn {
81
+ background: linear-gradient(45deg, #646cff, #0ea5e9);
82
+ color: white;
83
+ padding: 0.8rem 2rem;
84
+ font-size: 1rem;
85
+ border: none;
86
+ border-radius: 8px;
87
+ cursor: pointer;
88
+ font-weight: 600;
89
+ transition: transform 0.2s, box-shadow 0.2s;
90
+ margin-bottom: 1.5rem;
91
+ }
92
+
93
+ .action-btn:hover {
94
+ transform: translateY(-2px);
95
+ box-shadow: 0 10px 25px rgba(100, 108, 255, 0.3);
96
+ }
97
+
98
+ .action-btn:active {
99
+ transform: translateY(0);
100
+ }
101
+
102
+ .results {
103
+ background: #1a1a2e;
104
+ padding: 1.5rem;
105
+ border-radius: 8px;
106
+ border-left: 4px solid #646cff;
107
+ margin-top: 1rem;
108
+ font-family: 'Courier New', monospace;
109
+ word-break: break-all;
110
+ }
111
+
112
+ .results p {
113
+ margin: 0.8rem 0;
114
+ line-height: 1.6;
115
+ }
116
+
117
+ .results strong {
118
+ color: #646cff;
119
+ font-weight: 600;
120
+ }
121
+
122
+ footer {
123
+ text-align: center;
124
+ padding: 2rem;
125
+ border-top: 1px solid #444;
126
+ color: #a0a0a0;
127
+ }
128
+
129
+ footer a {
130
+ color: #646cff;
131
+ text-decoration: none;
132
+ margin: 0 0.5rem;
133
+ }
134
+
135
+ footer a:hover {
136
+ text-decoration: underline;
137
+ }
138
+
139
+ @media (max-width: 768px) {
140
+ header h1 {
141
+ font-size: 2rem;
142
+ }
143
+
144
+ .tabs {
145
+ gap: 0.5rem;
146
+ padding: 1rem;
147
+ }
148
+
149
+ .tabs button {
150
+ padding: 0.6rem 1rem;
151
+ font-size: 0.9rem;
152
+ }
153
+
154
+ .content {
155
+ padding: 1rem;
156
+ }
157
+
158
+ .section {
159
+ padding: 1.5rem;
160
+ }
161
+ }
@@ -0,0 +1,176 @@
1
+ import { useState } from 'react'
2
+ import { rng, deterministic, normal, exponential, meanTest, varianceTest, kolmogorovSmirnovTest } from '@cloudglides/nox'
3
+ import './App.css'
4
+
5
+ export default function App() {
6
+ const [tab, setTab] = useState('basic')
7
+ const [results, setResults] = useState({})
8
+
9
+ const runBasic = () => {
10
+ const r = rng()
11
+ setResults({
12
+ float: r.nextFloat().toFixed(6),
13
+ int: r.int(1, 100),
14
+ bool: r.bool(0.5),
15
+ range: r.range(10, 20, 2),
16
+ choice: r.choice(['apple', 'banana', 'cherry'])
17
+ })
18
+ }
19
+
20
+ const runBatch = () => {
21
+ const r = rng()
22
+ setResults({
23
+ floats: r.floats(5).map(x => x.toFixed(4)).join(', '),
24
+ ints: r.ints(5, 100).join(', '),
25
+ bools: r.bools(5).join(', ')
26
+ })
27
+ }
28
+
29
+ const runDistributions = () => {
30
+ const r = rng()
31
+ const samples = r.floats(100)
32
+ setResults({
33
+ normal: Array.from({length: 5}, () => normal(r).toFixed(4)).join(', '),
34
+ exponential: Array.from({length: 5}, () => exponential(r).toFixed(4)).join(', ')
35
+ })
36
+ }
37
+
38
+ const runStats = () => {
39
+ const r = rng()
40
+ const data = r.floats(1000)
41
+ const mean = meanTest(data)
42
+ const variance = varianceTest(data)
43
+ const ks = kolmogorovSmirnovTest(data)
44
+ setResults({
45
+ mean: mean.mean.toFixed(6),
46
+ variance: variance.variance.toFixed(6),
47
+ ksPass: ks.pass_0_05 ? 'PASS' : 'FAIL'
48
+ })
49
+ }
50
+
51
+ const runDeterministic = () => {
52
+ const r1 = deterministic(42)
53
+ const seq1 = r1.floats(5)
54
+ const r2 = deterministic(42)
55
+ const seq2 = r2.floats(5)
56
+ setResults({
57
+ seq1: seq1.map(x => x.toFixed(4)).join(', '),
58
+ seq2: seq2.map(x => x.toFixed(4)).join(', '),
59
+ identical: JSON.stringify(seq1) === JSON.stringify(seq2) ? 'YES' : 'NO'
60
+ })
61
+ }
62
+
63
+ return (
64
+ <div className="app">
65
+ <header>
66
+ <h1>nox - RNG Demo</h1>
67
+ <p>Unpredictable random number generator with multiple algorithms</p>
68
+ </header>
69
+
70
+ <div className="tabs">
71
+ <button className={tab === 'basic' ? 'active' : ''} onClick={() => setTab('basic')}>
72
+ Basic
73
+ </button>
74
+ <button className={tab === 'batch' ? 'active' : ''} onClick={() => setTab('batch')}>
75
+ Batch
76
+ </button>
77
+ <button className={tab === 'distributions' ? 'active' : ''} onClick={() => setTab('distributions')}>
78
+ Distributions
79
+ </button>
80
+ <button className={tab === 'stats' ? 'active' : ''} onClick={() => setTab('stats')}>
81
+ Statistics
82
+ </button>
83
+ <button className={tab === 'deterministic' ? 'active' : ''} onClick={() => setTab('deterministic')}>
84
+ Deterministic
85
+ </button>
86
+ </div>
87
+
88
+ <div className="content">
89
+ {tab === 'basic' && (
90
+ <div className="section">
91
+ <h2>Basic RNG Operations</h2>
92
+ <button onClick={runBasic} className="action-btn">Run</button>
93
+ <div className="results">
94
+ {results.float && (
95
+ <div>
96
+ <p><strong>nextFloat():</strong> {results.float}</p>
97
+ <p><strong>int(1, 100):</strong> {results.int}</p>
98
+ <p><strong>bool(0.5):</strong> {results.bool ? 'true' : 'false'}</p>
99
+ <p><strong>range(10, 20, 2):</strong> {results.range}</p>
100
+ <p><strong>choice():</strong> {results.choice}</p>
101
+ </div>
102
+ )}
103
+ </div>
104
+ </div>
105
+ )}
106
+
107
+ {tab === 'batch' && (
108
+ <div className="section">
109
+ <h2>Batch Operations</h2>
110
+ <button onClick={runBatch} className="action-btn">Run</button>
111
+ <div className="results">
112
+ {results.floats && (
113
+ <div>
114
+ <p><strong>floats(5):</strong> [{results.floats}]</p>
115
+ <p><strong>ints(5, 100):</strong> [{results.ints}]</p>
116
+ <p><strong>bools(5):</strong> [{results.bools}]</p>
117
+ </div>
118
+ )}
119
+ </div>
120
+ </div>
121
+ )}
122
+
123
+ {tab === 'distributions' && (
124
+ <div className="section">
125
+ <h2>Statistical Distributions</h2>
126
+ <button onClick={runDistributions} className="action-btn">Run</button>
127
+ <div className="results">
128
+ {results.normal && (
129
+ <div>
130
+ <p><strong>Normal(0, 1):</strong> [{results.normal}]</p>
131
+ <p><strong>Exponential(1):</strong> [{results.exponential}]</p>
132
+ </div>
133
+ )}
134
+ </div>
135
+ </div>
136
+ )}
137
+
138
+ {tab === 'stats' && (
139
+ <div className="section">
140
+ <h2>Statistical Tests</h2>
141
+ <button onClick={runStats} className="action-btn">Run on 1000 samples</button>
142
+ <div className="results">
143
+ {results.mean && (
144
+ <div>
145
+ <p><strong>Mean (expected 0.5):</strong> {results.mean}</p>
146
+ <p><strong>Variance (expected 0.083333):</strong> {results.variance}</p>
147
+ <p><strong>KS Test (α=0.05):</strong> {results.ksPass}</p>
148
+ </div>
149
+ )}
150
+ </div>
151
+ </div>
152
+ )}
153
+
154
+ {tab === 'deterministic' && (
155
+ <div className="section">
156
+ <h2>Deterministic Mode</h2>
157
+ <button onClick={runDeterministic} className="action-btn">Run with seed=42</button>
158
+ <div className="results">
159
+ {results.seq1 && (
160
+ <div>
161
+ <p><strong>Sequence 1:</strong> [{results.seq1}]</p>
162
+ <p><strong>Sequence 2:</strong> [{results.seq2}]</p>
163
+ <p><strong>Identical:</strong> {results.identical}</p>
164
+ </div>
165
+ )}
166
+ </div>
167
+ </div>
168
+ )}
169
+ </div>
170
+
171
+ <footer>
172
+ <p>Visit <a href="https://github.com/cloudglides/nox" target="_blank" rel="noreferrer">GitHub</a> | <a href="https://npmjs.com/package/@cloudglides/nox" target="_blank" rel="noreferrer">npm</a></p>
173
+ </footer>
174
+ </div>
175
+ )
176
+ }
@@ -0,0 +1,55 @@
1
+ :root {
2
+ font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3
+ line-height: 1.5;
4
+ font-weight: 400;
5
+ color: rgba(255, 255, 255, 0.87);
6
+ background-color: #242424;
7
+ }
8
+
9
+ * {
10
+ margin: 0;
11
+ padding: 0;
12
+ box-sizing: border-box;
13
+ }
14
+
15
+ body {
16
+ margin: 0;
17
+ min-width: 320px;
18
+ min-height: 100vh;
19
+ }
20
+
21
+ #root {
22
+ width: 100%;
23
+ min-height: 100vh;
24
+ }
25
+
26
+ button {
27
+ border-radius: 8px;
28
+ border: 1px solid transparent;
29
+ padding: 0.6em 1.2em;
30
+ font-size: 1em;
31
+ font-weight: 500;
32
+ font-family: inherit;
33
+ background-color: #1a1a1a;
34
+ cursor: pointer;
35
+ transition: border-color 0.25s;
36
+ }
37
+
38
+ button:hover {
39
+ border-color: #646cff;
40
+ }
41
+
42
+ button:focus,
43
+ button:focus-visible {
44
+ outline: 4px auto -webkit-focus-ring-color;
45
+ }
46
+
47
+ @media (prefers-color-scheme: light) {
48
+ :root {
49
+ color: #213547;
50
+ background-color: #ffffff;
51
+ }
52
+ button {
53
+ background-color: #f9f9f9;
54
+ }
55
+ }
@@ -0,0 +1,10 @@
1
+ import React from 'react'
2
+ import ReactDOM from 'react-dom/client'
3
+ import App from './App.jsx'
4
+ import './index.css'
5
+
6
+ ReactDOM.createRoot(document.getElementById('root')).render(
7
+ <React.StrictMode>
8
+ <App />
9
+ </React.StrictMode>,
10
+ )
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'vite'
2
+ import react from '@vitejs/plugin-react'
3
+
4
+ export default defineConfig({
5
+ plugins: [react()],
6
+ build: {
7
+ rollupOptions: {
8
+ external: ['perf_hooks', 'crypto']
9
+ }
10
+ }
11
+ })
package/package.json CHANGED
@@ -1,21 +1,22 @@
1
1
  {
2
2
  "name": "@cloudglides/nox",
3
- "version": "1.1.0",
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",
7
7
  "types": "src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "node": "./src/index.js",
11
+ "browser": "./src/index.browser.js",
12
+ "default": "./src/index.js"
13
+ }
14
+ },
8
15
  "scripts": {
9
16
  "test": "node test/basic.js",
10
17
  "bench": "node test/benchmark.js"
11
18
  },
12
- "keywords": [
13
- "random",
14
- "rng",
15
- "generator",
16
- "chaotic",
17
- "stochastic"
18
- ],
19
+ "keywords": ["random", "rng", "generator", "chaotic", "stochastic"],
19
20
  "author": "",
20
21
  "license": "ISC",
21
22
  "packageManager": "pnpm@10.24.0",
@@ -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';
package/src/core.js CHANGED
@@ -6,3 +6,5 @@ export { shuffle, pick, sample } from './utils/sequence.js';
6
6
  export { saveState, restoreState, cloneGenerator } from './utils/state.js';
7
7
  export { weightedPick, weightedSample, reservoirSample } from './utils/sampling.js';
8
8
  export { meanTest, varianceTest, kolmogorovSmirnovTest } from './utils/statistics.js';
9
+
10
+ export { combined, clearCryptoCache } from './utils/entropy.js';
@@ -0,0 +1,31 @@
1
+ export {
2
+ rng,
3
+ RNG,
4
+ deterministic,
5
+ normal,
6
+ exponential,
7
+ uniform,
8
+ poisson,
9
+ shuffle,
10
+ pick,
11
+ sample,
12
+ saveState,
13
+ restoreState,
14
+ cloneGenerator,
15
+ weightedPick,
16
+ weightedSample,
17
+ reservoirSample,
18
+ meanTest,
19
+ varianceTest,
20
+ kolmogorovSmirnovTest
21
+ } from './core.browser.js';
22
+
23
+ export {
24
+ Xorshift64,
25
+ Splitmix64,
26
+ PCG64,
27
+ MT19937,
28
+ Logistic,
29
+ Tent,
30
+ Mixer
31
+ } from './generators/index.js';
package/src/index.js CHANGED
@@ -29,3 +29,4 @@ export {
29
29
  Tent,
30
30
  Mixer
31
31
  } from './generators/index.js';
32
+
package/src/presets.js CHANGED
@@ -1,6 +1,13 @@
1
1
  import { RNG } from './rng.js';
2
2
  import { PCG64 } from './generators/index.js';
3
3
 
4
+ // Use browser or node entropy
5
+ const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
6
+ const entropyModule = isNode ?
7
+ await import('./utils/entropy.js') :
8
+ await import('./utils/entropy.browser.js');
9
+ const { combined: entropyFunc } = entropyModule;
10
+
4
11
  export const deterministic = (seed) => {
5
12
  if (seed === null || seed === undefined) {
6
13
  throw new TypeError('Deterministic mode requires an explicit seed');
@@ -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();