All files / random/base/xorshift32/lib main.js

100% Statements 90/90
100% Branches 1/1
100% Functions 0/0
100% Lines 90/90

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 911x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
* @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.
*/
 
'use strict';
 
// MODULES //
 
var factory = require( './factory.js' );
var randuint32 = require( './rand_uint32.js' );
 
 
// MAIN //
 
/**
* Generates a pseudorandom integer on the interval \\( [1,2^{32}-1) \\).
*
* ## Method
*
* Xorshift generators are a class of shift-register PRNGs that generate the next state by applying a sequence of bitwise XOR and shift operations, avoiding the use of multiplication or division.
*
* ```c
* state ^= state << a;
* state ^= state >> b;
* state ^= state << c;
* ```
*
* where \\( state \\) is a positive integer and \\( a, b, c \\) are carefully chosen shift constants.
*
* <!-- <note> -->
*
* In this implementation for 32-bit unsigned integers, the constants are:
*
* ```tex
* \begin{align*}
* a &= 13 \\
* b &= 17 \\
* c &= 5
* \end{align*}
* ```
*
* <!-- </note> -->
*
* The chosen parameters ensure a full-period generator, meaning it cycles through all nonzero 32-bit unsigned values before repeating.
*
* <!-- <note> -->
*
* Unlike linear congruential generators (LCGs), xorshift PRNGs do not rely on modular arithmetic, making them faster in software but requiring careful parameter selection to maintain statistical properties.
*
* <!-- </note> -->
*
* ## Notes
*
* -   Xorshift PRNGs are extremely fast but may fail some statistical tests if not combined with additional non-linear operations.
* -   This implementation follows the basic xorshift32 algorithm introduced by George Marsaglia.
*
* ## 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](https://doi.org/10.18637/jss.v008.i14).
*
* @function xorshift
* @type {PRNG}
* @returns {PositiveInteger} pseudorandom integer
*
* @example
* var v = xorshift32();
* // returns <number>
*/
var xorshift32 = factory({
	'seed': randuint32()
});
 
 
// EXPORTS //
 
module.exports = xorshift32;