@cloudglides/nox 1.1.0 → 1.1.1
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/example/README.md +26 -0
- package/example/index.html +13 -0
- package/example/package-lock.json +1636 -0
- package/example/package.json +20 -0
- package/example/src/App.css +161 -0
- package/example/src/App.jsx +176 -0
- package/example/src/index.css +55 -0
- package/example/src/main.jsx +10 -0
- package/example/vite.config.js +11 -0
- package/package.json +9 -8
- package/src/core.js +2 -0
- package/src/index.browser.js +31 -0
- package/src/index.js +1 -0
- package/src/presets.js +7 -0
- package/src/rng.js +6 -5
- package/src/utils/entropy.browser.js +63 -0
- package/src/utils/entropy.js +55 -12
|
@@ -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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudglides/nox",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
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",
|
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.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
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');
|
package/src/rng.js
CHANGED
|
@@ -61,7 +61,7 @@ export class RNG {
|
|
|
61
61
|
if (min > max) {
|
|
62
62
|
throw new Error('min must be less than or equal to max');
|
|
63
63
|
}
|
|
64
|
-
|
|
64
|
+
|
|
65
65
|
const count = Math.floor((max - min) / step) + 1;
|
|
66
66
|
const idx = this.nextInt(count);
|
|
67
67
|
return min + idx * step;
|
|
@@ -84,7 +84,7 @@ export class RNG {
|
|
|
84
84
|
if (typeof fn !== 'function') {
|
|
85
85
|
throw new TypeError('fn must be a function');
|
|
86
86
|
}
|
|
87
|
-
|
|
87
|
+
|
|
88
88
|
const result = [];
|
|
89
89
|
for (let i = 0; i < count; i++) {
|
|
90
90
|
result.push(fn(this, i));
|
|
@@ -99,7 +99,7 @@ export class RNG {
|
|
|
99
99
|
if (count < 0) {
|
|
100
100
|
throw new RangeError('count must be non-negative');
|
|
101
101
|
}
|
|
102
|
-
|
|
102
|
+
|
|
103
103
|
const result = new Array(count);
|
|
104
104
|
for (let i = 0; i < count; i++) {
|
|
105
105
|
result[i] = this.nextFloat();
|
|
@@ -114,7 +114,7 @@ export class RNG {
|
|
|
114
114
|
if (count < 0) {
|
|
115
115
|
throw new RangeError('count must be non-negative');
|
|
116
116
|
}
|
|
117
|
-
|
|
117
|
+
|
|
118
118
|
const result = new Array(count);
|
|
119
119
|
for (let i = 0; i < count; i++) {
|
|
120
120
|
result[i] = this.nextInt(max);
|
|
@@ -129,7 +129,7 @@ export class RNG {
|
|
|
129
129
|
if (count < 0) {
|
|
130
130
|
throw new RangeError('count must be non-negative');
|
|
131
131
|
}
|
|
132
|
-
|
|
132
|
+
|
|
133
133
|
const result = new Array(count);
|
|
134
134
|
for (let i = 0; i < count; i++) {
|
|
135
135
|
result[i] = this.bool(probability);
|
|
@@ -139,3 +139,4 @@ export class RNG {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
export const rng = () => new RNG();
|
|
142
|
+
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
let cryptoCache = null;
|
|
2
|
+
let cryptoCacheTime = 0;
|
|
3
|
+
|
|
4
|
+
export const fromPerformance = () => {
|
|
5
|
+
try {
|
|
6
|
+
if (typeof performance !== 'undefined' && performance.now) {
|
|
7
|
+
const t = performance.now();
|
|
8
|
+
return BigInt(Math.floor(t * 1000)) ^ BigInt(Math.floor(t * 1000000) % 1000000);
|
|
9
|
+
}
|
|
10
|
+
} catch {
|
|
11
|
+
// fallback
|
|
12
|
+
}
|
|
13
|
+
return BigInt(Date.now());
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const fromMemory = () => {
|
|
17
|
+
return BigInt(Date.now());
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const fromCrypto = (bytes = 8) => {
|
|
21
|
+
try {
|
|
22
|
+
const now = Date.now();
|
|
23
|
+
|
|
24
|
+
if (cryptoCache && (now - cryptoCacheTime) < 100) {
|
|
25
|
+
return cryptoCache;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let val = 0n;
|
|
29
|
+
|
|
30
|
+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
31
|
+
const arr = new Uint8Array(Math.max(bytes, 8));
|
|
32
|
+
crypto.getRandomValues(arr);
|
|
33
|
+
for (let i = 0; i < arr.length; i++) {
|
|
34
|
+
val = (val << 8n) | BigInt(arr[i]);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
return BigInt(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
cryptoCache = val;
|
|
41
|
+
cryptoCacheTime = now;
|
|
42
|
+
return val;
|
|
43
|
+
} catch {
|
|
44
|
+
return BigInt(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const combined = () => {
|
|
49
|
+
const perf = fromPerformance();
|
|
50
|
+
const mem = fromMemory();
|
|
51
|
+
const crypto = fromCrypto();
|
|
52
|
+
|
|
53
|
+
let mix = perf ^ mem ^ crypto;
|
|
54
|
+
mix = mix ^ (mix >> 33n);
|
|
55
|
+
mix = (mix * 0xff51afd7ed558ccdn) & ((1n << 64n) - 1n);
|
|
56
|
+
|
|
57
|
+
return mix !== 0n ? mix : 1n;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const clearCryptoCache = () => {
|
|
61
|
+
cryptoCache = null;
|
|
62
|
+
cryptoCacheTime = 0;
|
|
63
|
+
};
|