0
# Alternative PRNG Algorithms
1
2
seedrandom provides 6 high-performance alternative pseudorandom number generators, each with different statistical properties and performance characteristics. These algorithms are faster than the main ARC4-based generator but do not provide automatic entropy collection.
3
4
## Capabilities
5
6
### Alea PRNG
7
8
Johannes Baagøe's multiply-with-carry generator with excellent statistical properties and fast performance.
9
10
```javascript { .api }
11
/**
12
* Creates Alea PRNG - multiply-with-carry algorithm
13
* Period: ~2^116, passes all BigCrush tests
14
* Performance: ~1.95ns per call (0.9x native Math.random)
15
* @param {string|number} seed - Seed value (required, no auto-seeding)
16
* @param {object} options - Configuration options
17
* @returns {function} PRNG function with standard interface
18
*/
19
seedrandom.alea(seed, options);
20
```
21
22
**Usage Example:**
23
24
```javascript
25
var seedrandom = require('seedrandom');
26
27
// Create Alea PRNG
28
var rng = seedrandom.alea('hello.');
29
console.log(rng()); // 0.4783254903741181 (32-bit precision for alea)
30
console.log(rng.double()); // Higher precision: 0.8297006866124559
31
console.log(rng.int32()); // 32-bit integer: 1076136327
32
33
// With state management
34
var statefulRng = seedrandom.alea('test', { state: true });
35
var saved = statefulRng.state();
36
var replica = seedrandom.alea('', { state: saved });
37
```
38
39
### XOR128 PRNG
40
41
George Marsaglia's XOR-shift generator - simple and fast with good statistical properties.
42
43
```javascript { .api }
44
/**
45
* Creates XOR128 PRNG - pure XOR-shift algorithm
46
* Period: 2^128-1
47
* Performance: ~2.04ns per call (0.9x native Math.random)
48
* Known issues: Fails MatrixRank and LinearComp tests
49
* @param {string|number} seed - Seed value (required)
50
* @param {object} options - Configuration options
51
* @returns {function} PRNG function with standard interface
52
*/
53
seedrandom.xor128(seed, options);
54
```
55
56
**Usage Example:**
57
58
```javascript
59
var rng = seedrandom.xor128(12345);
60
console.log(rng()); // Fast random number (32-bit precision)
61
console.log(rng.quick()); // Same as main function for xor128
62
console.log(rng.int32()); // 32-bit integer output
63
console.log(rng.double()); // Higher precision (53-bit)
64
```
65
66
### XORWOW PRNG
67
68
Marsaglia's 160-bit XOR-shift combined with Weyl generator for improved randomness.
69
70
```javascript { .api }
71
/**
72
* Creates XORWOW PRNG - XOR-shift combined plus Weyl
73
* Period: 2^192-2^32
74
* Performance: ~2.40ns per call (1.1x native Math.random)
75
* Known issues: Fails CollisionOver, SimpPoker, and LinearComp tests
76
* @param {string|number} seed - Seed value (required)
77
* @param {object} options - Configuration options
78
* @returns {function} PRNG function with standard interface
79
*/
80
seedrandom.xorwow(seed, options);
81
```
82
83
### XorShift7 PRNG
84
85
Panneton and L'ecuyer's 7-shift generator with enhanced robustness through additional shifts.
86
87
```javascript { .api }
88
/**
89
* Creates XorShift7 PRNG - 7-shift generator with 256 bits
90
* Period: 2^256-1
91
* Performance: ~2.64ns per call (1.3x native Math.random)
92
* Quality: Passes BigCrush with no systematic failures
93
* @param {string|number} seed - Seed value (required)
94
* @param {object} options - Configuration options
95
* @returns {function} PRNG function with standard interface
96
*/
97
seedrandom.xorshift7(seed, options);
98
```
99
100
### XOR4096 PRNG
101
102
Richard Brent's 4096-bit XOR-shift with Weyl generator for extremely long periods.
103
104
```javascript { .api }
105
/**
106
* Creates XOR4096 PRNG - 4096-bit XOR-shift with Weyl
107
* Period: 2^4128-2^32 (extremely long period)
108
* Performance: ~2.40ns per call (1.1x native Math.random)
109
* Quality: Passes BigCrush with no systematic failures
110
* Use case: Many generators needing to avoid collisions
111
* @param {string|number} seed - Seed value (required)
112
* @param {object} options - Configuration options
113
* @returns {function} PRNG function with standard interface
114
*/
115
seedrandom.xor4096(seed, options);
116
```
117
118
### Tyche-i PRNG
119
120
Neves and Araujo's bit-shifting generator derived from the ChaCha stream cipher.
121
122
```javascript { .api }
123
/**
124
* Creates Tyche-i PRNG - ChaCha-derived bit-shifting generator
125
* Period: ~2^127
126
* Performance: ~2.32ns per call (1.1x native Math.random)
127
* Quality: Passes BigCrush with no systematic failures
128
* @param {string|number} seed - Seed value (required)
129
* @param {object} options - Configuration options
130
* @returns {function} PRNG function with standard interface
131
*/
132
seedrandom.tychei(seed, options);
133
```
134
135
## Algorithm Comparison
136
137
| Algorithm | Period | Performance | BigCrush Status | Best Use Case |
138
|-------------|---------------|-------------|---------------------------|----------------------------------|
139
| **alea** | ~2^116 | 0.9x | Passes all tests | General purpose, fastest |
140
| **xor128** | 2^128-1 | 0.9x | Fails some tests | Speed-critical applications |
141
| **tychei** | ~2^127 | 1.1x | No failures | Cryptographic applications |
142
| **xorwow** | 2^192-2^32 | 1.1x | Fails some tests | Balanced speed/quality |
143
| **xor4096** | 2^4128-2^32 | 1.1x | No failures | Many parallel generators |
144
| **xorshift7**| 2^256-1 | 1.3x | No failures | High-quality randomness needed |
145
146
*Performance relative to native Math.random() on Node.js v0.12.2*
147
148
## Common Usage Patterns
149
150
### Algorithm Selection
151
152
```javascript
153
var seedrandom = require('seedrandom');
154
155
// For fastest performance
156
var fast = seedrandom.alea('speed-test');
157
158
// For cryptographic quality
159
var crypto = seedrandom.tychei('crypto-seed');
160
161
// For many parallel generators
162
var parallel1 = seedrandom.xor4096('seed-1');
163
var parallel2 = seedrandom.xor4096('seed-2');
164
// Extremely unlikely to have overlapping sequences
165
166
// For highest quality randomness
167
var quality = seedrandom.xorshift7('quality-seed');
168
```
169
170
### Browser Loading (Individual Algorithms)
171
172
```html
173
<!-- Load individual algorithm -->
174
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/lib/alea.min.js"></script>
175
<script>
176
var rng = alea('browser-seed');
177
console.log(rng()); // Uses Alea algorithm directly
178
</script>
179
180
<!-- Multiple algorithms -->
181
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/lib/alea.min.js"></script>
182
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/lib/xor128.min.js"></script>
183
<script>
184
var aleaRng = alea('seed1');
185
var xorRng = xor128('seed2');
186
</script>
187
```
188
189
### Performance Testing
190
191
```javascript
192
var seedrandom = require('seedrandom');
193
194
// Performance comparison
195
var algorithms = [
196
{ name: 'alea', fn: seedrandom.alea('test') },
197
{ name: 'xor128', fn: seedrandom.xor128('test') },
198
{ name: 'tychei', fn: seedrandom.tychei('test') },
199
{ name: 'native', fn: Math.random }
200
];
201
202
algorithms.forEach(function(alg) {
203
var start = Date.now();
204
for (var i = 0; i < 1000000; i++) {
205
alg.fn();
206
}
207
var time = Date.now() - start;
208
console.log(alg.name + ': ' + time + 'ms');
209
});
210
```
211
212
## Important Notes
213
214
- **No Auto-seeding**: Alternative PRNGs require explicit seeds (cannot be null/undefined)
215
- **Manual Entropy**: Unlike main seedrandom function, these don't automatically collect entropy
216
- **Same Interface**: All algorithms return PRNG functions with identical method signatures
217
- **Platform Consistency**: Results are identical across different JavaScript environments
218
- **State Management**: All algorithms support optional state save/restore functionality
219
220
## Error Handling
221
222
```javascript
223
// These will throw errors or produce poor randomness:
224
try {
225
var badRng = seedrandom.alea(); // No seed provided
226
} catch (e) {
227
console.log('Seed required for alternative PRNGs');
228
}
229
230
// Proper usage:
231
var goodRng = seedrandom.alea('proper-seed');
232
var autoSeedRng = seedrandom(); // Main function handles auto-seeding
233
```