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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | 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 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 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 3x 3x 6x 9x 1x 1x 5x 9x 1x 1x 4x 9x 4x 9x 4x 4x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 57x 7x 7x 50x 57x 9x 9x 6x 6x 9x 44x 57x 15x 15x 15x 15x 7x 7x 8x 8x 15x 5x 5x 3x 3x 3x 3x 3x 18x 18x 3x 3x 15x 32x 57x 29x 28x 28x 28x 28x 2x 2x 28x 20x 26x 6x 6x 22x 28x 1x 1x 29x 1x 1x 1x 1x 29x 59x 2x 2x 2x 2x 28x 59x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 28x 28x 28x 28x 28x 28x 28x 28x 22552x 22552x 22552x 22552x 22552x 22552x 22552x 22552x 22552x 22552x 22552x 22552x 22552x 22552x 28x 28x 28x 28x 28x 28x 28x 28x 11200x 11200x 28x 28x 28x 28x 28x 28x 28x 28x 3x 3x 3x 3x 18x 18x 3x 3x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 28x 59x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 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.
*/
/**
* Xorshift128+ pseudorandom number generator (PRNG).
*
* ## Algorithm
*
* The Xorshift128+ algorithm generates 64-bit pseudorandom numbers by
* maintaining two 64-bit state values and performing XOR and shift operations.
*
* ## Notes
*
* - Uses JavaScript BigInt for 64-bit arithmetic.
* - State consists of two 64-bit values: s0 and s1.
* - Period: approximately 2^128.
*
* @example
* var xorshift128plus = require( '@stdlib/random/base/xorshift128plus' );
*
* var rng = xorshift128plus({
* 'seed': BigInt(12345)
* });
*
* var v = rng();
* // returns <bigint>
*/
'use strict';
// MODULES //
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
var setReadWriteAccessor = require( '@stdlib/utils/define-nonenumerable-read-write-accessor' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isObject = require( '@stdlib/assert/is-plain-object' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' );
var isBigInt = require( '@stdlib/assert/is-bigint' );
var BigInt = require( '@stdlib/bigint/ctor' );
var Number = require( '@stdlib/number/ctor' );
var randu = require( '@stdlib/random/base/randu' );
var floor = require( '@stdlib/math/base/special/floor' );
var format = require( '@stdlib/string/format' );
// VARIABLES //
/* eslint-disable no-magic-numbers */
/**
* State array schema version.
*
* @private
* @type {bigint}
*/
var STATE_ARRAY_VERSION = BigInt( 1 );
/**
* Number of state elements (2 × 64-bit values).
*
* @private
* @type {bigint}
*/
var NUM_STATE_ELEMENTS = BigInt( 2 );
/**
* Total state array length:.
* [version | state_length | state[0] | state[1] | seed_length | seed]
*
* @private
* @type {number}
*/
var STATE_ARRAY_SIZE = 6;
/**
* State array indices.
*
* @private
* @type {number}
*/
var VERSION_INDEX = 0;
var STATE_LENGTH_INDEX = 1;
var STATE_OFFSET = 2;
var SEED_LENGTH_INDEX = 4;
var SEED_OFFSET = 5;
/**
* Maximum value for a 64-bit unsigned integer.
*
* @private
* @type {bigint}
*/
var UINT64_MAX_BIGINT = BigInt( '0xffffffffffffffff' );
/**
* 2^64 as BigInt for normalization.
*
* @private
* @type {bigint}
*/
var TWO_64 = BigInt( '0x10000000000000000' );
/**
* Normalization constant for converting uint64 to [0,1).
*
* @private
* @type {number}
*/
var NORMALIZATION_CONSTANT = 1.0 / Number( TWO_64 );
/* eslint-enable no-magic-numbers */
// FUNCTIONS //
/**
* Initializes the internal PRNG state from a seed.
*
* @private
* @param {bigint} seed - seed value
* @returns {Array} state array with two 64-bit values
*/
function initializeState( seed ) {
var MULTIPLIER_1 = BigInt( '0x9e3779b97f4a7c15' );
var MULTIPLIER_2 = BigInt( '0xbf58476d1ce4e5b9' );
var state = [ BigInt( 0 ), BigInt( 0 ) ];
state[ 0 ] = (seed ^ MULTIPLIER_1) & UINT64_MAX_BIGINT;
state[ 1 ] = (seed ^ MULTIPLIER_2) & UINT64_MAX_BIGINT;
if ( state[ 0 ] === BigInt( 0 ) && state[ 1 ] === BigInt( 0 ) ) {
state[ 0 ] = BigInt( 1 );
}
return state;
}
/**
* Verifies state array integrity.
*
* @private
* @param {Array} stateArray - state array
* @returns {(Error|null)} error or null
*/
function verifyState( stateArray ) {
if ( !Array.isArray( stateArray ) ) {
return new TypeError( 'invalid state array. State must be an Array.' );
}
if ( stateArray.length !== STATE_ARRAY_SIZE ) {
return new RangeError(format('invalid state array. Expected length %u. Got %u.', STATE_ARRAY_SIZE, stateArray.length));
}
if ( stateArray[ VERSION_INDEX ] !== STATE_ARRAY_VERSION ) {
return new RangeError(format('invalid state array. Unsupported version %u.', stateArray[ VERSION_INDEX ]));
}
if ( stateArray[ STATE_LENGTH_INDEX ] !== NUM_STATE_ELEMENTS ) {
return new RangeError(format('invalid state array. Expected state length %u. Got %u.', NUM_STATE_ELEMENTS, stateArray[ STATE_LENGTH_INDEX ]));
}
if ( typeof stateArray[ STATE_OFFSET ] !== 'bigint' ) {
return new TypeError( 'invalid state array. State[0] must be a BigInt.' );
}
if ( typeof stateArray[ STATE_OFFSET + 1 ] !== 'bigint' ) {
return new TypeError( 'invalid state array. State[1] must be a BigInt.' );
}
return null;
}
// MAIN //
/**
* Returns an Xorshift128+ pseudorandom number generator.
*
* @param {Object} [options] - configuration object
* @param {(number|bigint)} [options.seed] - seed value
* @param {Array} [options.state] - generator state
* @param {boolean} [options.copy=true] - copy state
* @throws {TypeError} options must be an object
* @throws {TypeError} invalid option value
* @throws {RangeError} invalid state array
* @returns {Function} PRNG function
*/
function factory( options ) {
var STATE;
var state;
var seed;
var opts;
var err;
var tmp;
var i;
opts = {};
if ( arguments.length ) {
if ( !isObject( options ) ) {
throw new TypeError(format('invalid argument. Options must be an object. Received: %s', typeof options));
}
if ( hasOwnProp( options, 'copy' ) ) {
opts.copy = options.copy;
if ( !isBoolean( options.copy ) ) {
throw new TypeError(format('invalid option. `copy` must be a boolean. Received: %s', typeof options.copy));
}
}
if ( hasOwnProp( options, 'state' ) ) {
STATE = options.state;
opts.state = true;
if ( !Array.isArray( STATE ) ) {
throw new TypeError('invalid option. `state` must be an Array.');
}
err = verifyState( STATE );
if ( err ) {
throw err;
}
if ( opts.copy !== false ) {
tmp = [];
tmp.length = STATE_ARRAY_SIZE;
for ( i = 0; i < STATE_ARRAY_SIZE; i++ ) {
tmp[ i ] = STATE[ i ];
}
STATE = tmp;
}
}
if ( !opts.state ) {
if ( hasOwnProp( options, 'seed' ) ) {
seed = options.seed;
opts.seed = true;
if ( typeof seed === 'number' ) {
if ( !isNonNegativeInteger( seed ) ) {
throw new TypeError('invalid option. `seed` must be a non-negative integer or BigInt.');
}
seed = BigInt( seed );
} else if ( isBigInt( seed ) ) {
if ( seed < BigInt( 0 ) ) {
throw new RangeError('invalid option. `seed` must be a non-negative BigInt.');
}
} else {
throw new TypeError('invalid option. `seed` must be a non-negative integer or BigInt.');
}
if ( seed === BigInt( 0 ) ) {
seed = BigInt( 1 );
}
} else {
seed = BigInt( floor( randu() * 0x100000000 ) ) * BigInt( '0x100000000' );
seed |= BigInt( floor( randu() * 0x100000000 ) );
if ( seed === BigInt( 0 ) ) {
seed = BigInt( 1 );
}
}
}
} else {
seed = BigInt( floor( randu() * 0x100000000 ) ) * BigInt( '0x100000000' );
seed |= BigInt( floor( randu() * 0x100000000 ) );
if ( seed === BigInt( 0 ) ) {
seed = BigInt( 1 );
}
}
if ( STATE === void 0 ) {
state = initializeState( seed );
STATE = [];
STATE.length = STATE_ARRAY_SIZE;
STATE[ VERSION_INDEX ] = STATE_ARRAY_VERSION;
STATE[ STATE_LENGTH_INDEX ] = NUM_STATE_ELEMENTS;
STATE[ STATE_OFFSET ] = state[ 0 ];
STATE[ STATE_OFFSET + 1 ] = state[ 1 ];
STATE[ SEED_LENGTH_INDEX ] = BigInt( 1 );
STATE[ SEED_OFFSET ] = seed;
}
/**
* Generates a pseudorandom 64-bit unsigned integer.
*
* @private
* @returns {bigint} pseudorandom 64-bit integer
*/
function next() {
var s1 = STATE[ STATE_OFFSET ];
var s0 = STATE[ STATE_OFFSET + 1 ];
STATE[ STATE_OFFSET ] = s0;
s1 ^= (s1 << BigInt( 23 ));
s1 ^= (s1 >> BigInt( 18 ));
s1 ^= s0;
s0 ^= (s0 >> BigInt( 5 ));
STATE[ STATE_OFFSET + 1 ] = (s1 ^ s0) & UINT64_MAX_BIGINT;
return ((s1 ^ s0) + s0) & UINT64_MAX_BIGINT;
}
/**
* Generates a pseudorandom number on [0, 1).
*
* @private
* @returns {number} pseudorandom number in [0, 1)
*/
function norm() {
return Number( next() ) * NORMALIZATION_CONSTANT;
}
/**
* Returns the current state.
*
* @private
* @returns {Array} state array
*/
function getState() {
var out = [];
var i;
out.length = STATE_ARRAY_SIZE;
for ( i = 0; i < STATE_ARRAY_SIZE; i++ ) {
out[ i ] = STATE[ i ];
}
return out;
}
/**
* Sets the state.
*
* @private
* @param {Array} s - new state
* @throws {TypeError} invalid state type
* @throws {RangeError} invalid state
*/
function setState( s ) {
var e;
var i;
if ( !Array.isArray( s ) ) {
throw new TypeError('invalid argument. `state` must be an Array.');
}
e = verifyState( s );
if ( e ) {
throw e;
}
if ( opts.copy === false ) {
if ( opts.state && s.length === STATE.length ) {
for ( i = 0; i < STATE_ARRAY_SIZE; i++ ) {
STATE[ i ] = s[ i ];
}
} else {
STATE = s;
opts.state = true;
}
} else {
if ( s.length !== STATE.length ) {
STATE = [];
STATE.length = STATE_ARRAY_SIZE;
}
for ( i = 0; i < STATE_ARRAY_SIZE; i++ ) {
STATE[ i ] = s[ i ];
}
}
}
/**
* Returns a copy of the PRNG.
*
* @private
* @returns {Function} copy
*/
function copy() {
return factory({
'state': getState(),
'copy': true
});
}
/**
* Serializes the state.
*
* @private
* @returns {Object} JSON object
*/
function toJSON() {
return {
'type': 'PRNG',
'name': 'xorshift128+',
'state': Array.from( STATE ),
'params': []
};
}
/**
* Returns the seed.
*
* @private
* @returns {bigint} seed
*/
function getSeed() {
return STATE[ SEED_OFFSET ];
}
setReadOnly( next, 'NAME', 'xorshift128+' );
setReadOnly( next, 'normalized', norm );
setReadOnly( next, 'copy', copy );
setReadOnly( next, 'toJSON', toJSON );
setReadOnlyAccessor( next, 'seed', getSeed );
setReadWriteAccessor( next, 'state', getState, setState );
return next;
}
// EXPORTS //
module.exports = factory;
|