Skip to content

Commit 43fc630

Browse files
feat: implement xorshift32 PRNG
1 parent 67f1e39 commit 43fc630

16 files changed

Lines changed: 2766 additions & 260 deletions

File tree

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Xorshift32
22+
23+
> A 32-bit [xorshift32][xorshift32] pseudorandom number generator.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var xorshift32 = require( '@stdlib/random/base/xorshift32' );
31+
```
32+
33+
#### xorshift32()
34+
35+
Returns a pseudorandom unsigned 32-bit integer on the interval `[0, 4294967295]`
36+
37+
```javascript
38+
var r = xorshift32();
39+
// returns <number>
40+
```
41+
42+
#### xorshift32.normalized()
43+
44+
Returns a pseudorandom number on the interval [0, 1) with 53-bit precision.
45+
46+
```javascript
47+
var r = xorshift32.normalized();
48+
// returns <number>
49+
```
50+
51+
#### xorshift32.factory( \[options] )
52+
53+
Returns a 32-bit [xorshift32][xorshift32] pseudorandom number generator.
54+
55+
```javascript
56+
var rand = xorshift32.factory();
57+
```
58+
59+
The function accepts the following options:
60+
61+
- **seed**: pseudorandom number generator seed. Must be an integer on the interval `[1, 4294967295]`. If a seed is not provided, one is automatically generated.
62+
- **state**: a `Uint32Array` containing pseudorandom number generator state. If provided, the `seed` option is ignored.
63+
- **copy**: `boolean` indicating whether to copy a provided pseudorandom number generator state. Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that a returned generator has exclusive control over its internal state. Default: `true` .
64+
65+
To seed the generator, provide a nonzero integer on the interval `[1, 4294967295]`:
66+
67+
```javascript
68+
var rand = xorshift32.factory({
69+
'seed': 1234
70+
});
71+
72+
var r = rand();
73+
// returns <number>
74+
```
75+
To use a custom generator state, provide a `Uint32Array`:
76+
77+
```javascript
78+
var rand1 = xorshift32.factory();
79+
var state = rand1.state;
80+
81+
var rand2 = xorshift32.factory({
82+
'state': state,
83+
'copy': false
84+
});
85+
86+
var r = rand2();
87+
// returns <number>
88+
```
89+
90+
#### xorshift32.NAME
91+
92+
The generator name.
93+
94+
```javascript
95+
var str = xorshift32.NAME;
96+
// returns 'xorshift32'
97+
```
98+
99+
#### xorshift32.MIN
100+
101+
Minimum possible value.
102+
103+
```javascript
104+
var min = xorshift32.MIN;
105+
// returns 0
106+
```
107+
108+
#### xorshift32.MAX
109+
110+
Maximum possible value.
111+
112+
```javascript
113+
var max = xorshift32.MAX;
114+
// returns 4294967295
115+
```
116+
117+
#### xorshift32.seed
118+
119+
The value used to seed `xorshift32()`.
120+
121+
```javascript
122+
// Generate pseudorandom values...
123+
var r;
124+
var i;
125+
for ( i = 0; i < 100; i++ ) {
126+
r = xorshift32();
127+
}
128+
129+
// Generate the same pseudorandom values...
130+
var rand = xorshift32.factory({
131+
'seed': xorshift32.seed
132+
});
133+
for ( i = 0; i < 100; i++ ) {
134+
r = rand();
135+
}
136+
```
137+
138+
#### xorshift32.seedLength
139+
140+
Length of generator seed.
141+
142+
```javascript
143+
var len = xorshift32.seedLength;
144+
// returns 1
145+
```
146+
147+
#### xorshift32.state
148+
149+
The current pseudorandom number generator state.
150+
151+
```javascript
152+
var state = xorshift32.state;
153+
// returns <Uint32Array>
154+
```
155+
156+
#### xorshift32.stateLength
157+
158+
Length of generator state.
159+
160+
```javascript
161+
var len = xorshift32.stateLength;
162+
// returns 3
163+
```
164+
165+
#### xorshift32.byteLength
166+
167+
Size of generator state in bytes.
168+
169+
```javascript
170+
var nbytes = xorshift32.byteLength;
171+
// returns 12
172+
```
173+
174+
#### xorshift32.toJSON()
175+
176+
Serializes the pseudorandom number generator as a JSON object.
177+
178+
```javascript
179+
var o = xorshift32.toJSON();
180+
// returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] }
181+
```
182+
183+
</section>
184+
185+
<!-- /.usage -->
186+
187+
<section class="notes">
188+
189+
## Notes
190+
191+
- [Xorshift32][xorshift32] is **not** cryptographically secure. Do not use it for cryptographic operations.
192+
- The generator uses a single 32-bit internal state, making it extremely lightweight and fast.
193+
- While the generator is very efficient, its statistical quality is lower than generators like Mersenne Twister. It is suitable for simulations and statistical applications, but not for security-sensitive use cases.
194+
- The period of Xorshift32 is approximately 2^32 - 1.
195+
- The generator state can be explicitly accessed and saved for reproducibility.
196+
197+
</section>
198+
199+
<!-- /.notes -->
200+
201+
<section class="examples">
202+
203+
## Examples
204+
205+
<!-- eslint no-undef: "error" -->
206+
207+
```javascript
208+
var xorshift32 = require( '@stdlib/random/base/xorshift32' );
209+
210+
// Generate pseudorandom numbers...
211+
var i;
212+
for ( i = 0; i < 100; i++ ) {
213+
console.log( xorshift32() );
214+
}
215+
216+
// Create a new pseudorandom number generator...
217+
var seed = 1234;
218+
var rand = xorshift32.factory({
219+
'seed': seed
220+
});
221+
for ( i = 0; i < 100; i++ ) {
222+
console.log( rand() );
223+
}
224+
225+
// Create another pseudorandom number generator using a previous seed...
226+
rand = xorshift32.factory({
227+
'seed': xorshift32.seed
228+
});
229+
for ( i = 0; i < 100; i++ ) {
230+
console.log( rand() );
231+
}
232+
```
233+
234+
</section>
235+
236+
<!-- /.examples -->
237+
238+
* * *
239+
240+
<section class="references">
241+
242+
## References
243+
244+
- George Marsaglia. 2003. "Xorshift RNGs." *Journal of Statistical Software*, 8(14).
245+
246+
</section>
247+
248+
<!-- /.references -->
249+
250+
<!-- Section for related stdlib packages. Do not manually edit this section, as it is automatically populated. -->
251+
252+
<section class="related">
253+
254+
* * *
255+
256+
## See Also
257+
258+
- <span class="package-name">[@stdlib/random/base/minstd][@stdlib/random/base/minstd]</span><span class="delimiter">: </span><span class="description">A linear congruential pseudorandom number generator (LCG) based on Park and Miller.</span>
259+
- <span class="package-name">[@stdlib/random/base/randi][@stdlib/random/base/randi]</span><span class="delimiter">: </span><span class="description">pseudorandom numbers having integer values.</span>
260+
261+
</section>
262+
263+
<!-- /.related -->
264+
265+
<!-- Section for all links. Make sure to keep an empty line after the section element and another before the /section close. -->
266+
267+
<section class="links">
268+
269+
[xorshift32]: https://en.wikipedia.org/wiki/Xorshift
270+
271+
<!-- <related-links> -->
272+
273+
[@stdlib/random/base/minstd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/minstd
274+
275+
[@stdlib/random/base/randi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/randi
276+
277+
<!-- </related-links> -->
278+
279+
</section>
280+
281+
<!-- /.links -->

0 commit comments

Comments
 (0)