-
-
Notifications
You must be signed in to change notification settings - Fork 903
feat(random): add a new pseudorandom number generator random/base/xorshift32
#6333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
impawstarlight
wants to merge
6
commits into
stdlib-js:develop
Choose a base branch
from
impawstarlight:feature/xorshift
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
001eadf
chore: copy files from `random/base/minstd`
impawstarlight 3386039
feat(random): implement xorshift generator
impawstarlight 41768f5
docs: update docs and comments
impawstarlight 230d7af
chore: replace XORSHIFT with Xorshift32 and remove related section fr…
impawstarlight 03a165d
refactor: rename package `xorshift` to `xorshift32`
impawstarlight f024aa1
chore: cleanup
impawstarlight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
346 changes: 346 additions & 0 deletions
346
lib/node_modules/@stdlib/random/base/xorshift/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,346 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# XORSHIFT | ||
|
||
> A 32-bit xorshift pseudorandom number generator ([XORSHIFT][xorshift]) based on George Marsaglia. | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var xorshift = require( '@stdlib/random/base/xorshift' ); | ||
``` | ||
|
||
#### xorshift() | ||
|
||
Returns a pseudorandom integer on the interval `[1, 4294967295]`. | ||
|
||
```javascript | ||
var r = xorshift(); | ||
// returns <number> | ||
``` | ||
|
||
#### xorshift.normalized() | ||
|
||
Returns a pseudorandom number on the interval `[0,1)`. | ||
|
||
```javascript | ||
var r = xorshift.normalized(); | ||
// returns <number> | ||
``` | ||
|
||
#### xorshift.factory( \[options] ) | ||
|
||
Returns a 32-bit xorshift pseudorandom number generator ([XORSHIFT][xorshift]). | ||
|
||
```javascript | ||
var rand = xorshift.factory(); | ||
``` | ||
|
||
The function accepts the following `options`: | ||
|
||
- **seed**: pseudorandom number generator seed. | ||
- **state**: an [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option. | ||
- **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`. | ||
|
||
By default, a random integer is used to seed the returned generator. To seed the generator, provide either an `integer` on the interval `[1, 4294967295]` | ||
|
||
```javascript | ||
var rand = xorshift.factory({ | ||
'seed': 1234 | ||
}); | ||
|
||
var r = rand(); | ||
// returns 332584831 | ||
``` | ||
|
||
or, for arbitrary length seeds, an array-like `object` containing unsigned 32-bit integers | ||
|
||
```javascript | ||
var Uint32Array = require( '@stdlib/array/uint32' ); | ||
|
||
var rand = xorshift.factory({ | ||
'seed': new Uint32Array( [ 1234 ] ) | ||
}); | ||
|
||
var r = rand(); | ||
// returns 332584831 | ||
``` | ||
|
||
To return a generator having a specific initial state, set the generator `state` option. | ||
|
||
```javascript | ||
// Generate pseudorandom numbers, thus progressing the generator state: | ||
var r; | ||
var i; | ||
for ( i = 0; i < 1000; i++ ) { | ||
r = xorshift(); | ||
} | ||
|
||
// Create a new PRNG initialized to the current state of `xorshift`: | ||
var rand = xorshift.factory({ | ||
'state': xorshift.state | ||
}); | ||
|
||
// Test that the generated pseudorandom numbers are the same: | ||
var bool = ( rand() === xorshift() ); | ||
// returns true | ||
``` | ||
|
||
#### xorshift.NAME | ||
|
||
The generator name. | ||
|
||
```javascript | ||
var str = xorshift.NAME; | ||
// returns 'xorshift' | ||
``` | ||
|
||
#### xorshift.MIN | ||
|
||
Minimum possible value. | ||
|
||
```javascript | ||
var min = xorshift.MIN; | ||
// returns 1 | ||
``` | ||
|
||
#### xorshift.MAX | ||
|
||
Maximum possible value. | ||
|
||
```javascript | ||
var max = xorshift.MAX; | ||
// returns 4294967295 | ||
``` | ||
|
||
#### xorshift.seed | ||
|
||
The value used to seed `xorshift()`. | ||
|
||
```javascript | ||
// Generate pseudorandom values... | ||
var r; | ||
var i; | ||
for ( i = 0; i < 100; i++ ) { | ||
r = xorshift(); | ||
} | ||
|
||
// Generate the same pseudorandom values... | ||
var rand = xorshift.factory({ | ||
'seed': xorshift.seed | ||
}); | ||
for ( i = 0; i < 100; i++ ) { | ||
r = rand(); | ||
} | ||
``` | ||
|
||
#### xorshift.seedLength | ||
|
||
Length of generator seed. | ||
|
||
```javascript | ||
var len = xorshift.seedLength; | ||
// returns <number> | ||
``` | ||
|
||
#### xorshift.state | ||
|
||
Writable property for getting and setting the generator state. | ||
|
||
```javascript | ||
var r = xorshift(); | ||
// returns <number> | ||
|
||
r = xorshift(); | ||
// returns <number> | ||
|
||
// ... | ||
|
||
// Get the current state: | ||
var state = xorshift.state; | ||
// returns <Uint32Array> | ||
|
||
r = xorshift(); | ||
// returns <number> | ||
|
||
r = xorshift(); | ||
// returns <number> | ||
|
||
// Reset the state: | ||
xorshift.state = state; | ||
|
||
// Replay the last two pseudorandom numbers: | ||
r = xorshift(); | ||
// returns <number> | ||
|
||
r = xorshift(); | ||
// returns <number> | ||
|
||
// ... | ||
``` | ||
|
||
#### xorshift.stateLength | ||
|
||
Length of generator state. | ||
|
||
```javascript | ||
var len = xorshift.stateLength; | ||
// returns <number> | ||
``` | ||
|
||
#### xorshift.byteLength | ||
|
||
Size (in bytes) of generator state. | ||
|
||
```javascript | ||
var sz = xorshift.byteLength; | ||
// returns <number> | ||
``` | ||
|
||
#### xorshift.toJSON() | ||
|
||
Serializes the pseudorandom number generator as a JSON object. | ||
|
||
```javascript | ||
var o = xorshift.toJSON(); | ||
// returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] } | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- The generator has a period of `2^32-1`. | ||
- Like all [LFSR][lfsr]s, the parameters have to be chosen very carefully in order to achieve a long period. For execution in software, xorshift generators are among the fastest PRNGs, requiring very small code and state. However, they do not pass every statistical test without further refinement. | ||
- If PRNG state is "shared" (meaning a state array was provided during PRNG creation and **not** copied) and one sets the generator state to a state array having a different length, the PRNG does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize PRNG output according to the new shared state array, the state array for **each** relevant PRNG must be **explicitly** set. | ||
- If PRNG state is "shared" and one sets the generator state to a state array of the same length, the PRNG state is updated (along with the state of all other PRNGs sharing the PRNG's state array). | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var xorshift = require( '@stdlib/random/base/xorshift' ); | ||
|
||
// Generate pseudorandom numbers... | ||
var i; | ||
for ( i = 0; i < 100; i++ ) { | ||
console.log( xorshift() ); | ||
} | ||
|
||
// Create a new pseudorandom number generator... | ||
var seed = 1234; | ||
var rand = xorshift.factory({ | ||
'seed': seed | ||
}); | ||
for ( i = 0; i < 100; i++ ) { | ||
console.log( rand() ); | ||
} | ||
|
||
// Create another pseudorandom number generator using a previous seed... | ||
rand = xorshift.factory({ | ||
'seed': xorshift.seed | ||
}); | ||
for ( i = 0; i < 100; i++ ) { | ||
console.log( rand() ); | ||
} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
* * * | ||
|
||
<section class="references"> | ||
|
||
## References | ||
|
||
- Marsaglia, G. 2003. "Xorshift RNGs." _Journal of Statistical Software_ 8 (14). Los Angeles, CA, USA: American Statistical Association: 1–6. doi:[10.18637/jss.v008.i14][@marsaglia:2003]. | ||
|
||
</section> | ||
|
||
<!-- /.references --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
* * * | ||
|
||
## See Also | ||
|
||
<!-- | ||
- <span class="package-name">[`@stdlib/random/array/xorshift`][@stdlib/random/array/xorshift]</span><span class="delimiter">: </span><span class="description">create an array containing pseudorandom numbers generated using a 32-bit xorshift pseudorandom number generator based on George Marsaglia.</span> | ||
- <span class="package-name">[`@stdlib/random/iter/xorshift`][@stdlib/random/iter/xorshift]</span><span class="delimiter">: </span><span class="description">create an iterator for a 32-bit xorshift pseudorandom number generator based on George Marsaglia.</span> | ||
- <span class="package-name">[`@stdlib/random/streams/xorshift`][@stdlib/random/streams/xorshift]</span><span class="delimiter">: </span><span class="description">create a readable stream for a 32-bit xorshift pseudorandom number generator based on George Marsaglia.</span> | ||
--> | ||
|
||
- <span class="package-name">[`@stdlib/random/base/mt19937`][@stdlib/random/base/mt19937]</span><span class="delimiter">: </span><span class="description">A 32-bit Mersenne Twister pseudorandom number generator.</span> | ||
- <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> | ||
impawstarlight marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[xorshift]: https://en.wikipedia.org/wiki/Xorshift | ||
|
||
[lfsr]: https://en.wikipedia.org/wiki/Linear-feedback_shift_register | ||
|
||
[@marsaglia:2003]: https://doi.org/10.18637/jss.v008.i14 | ||
|
||
[@stdlib/array/uint32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint32 | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- | ||
[@stdlib/random/array/xorshift]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/array/xorshift | ||
|
||
[@stdlib/random/iter/xorshift]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/iter/xorshift | ||
|
||
[@stdlib/random/streams/xorshift]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/streams/xorshift | ||
--> | ||
|
||
[@stdlib/random/base/mt19937]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/mt19937 | ||
|
||
[@stdlib/random/base/randi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/randi | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.