Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 267 additions & 0 deletions lib/node_modules/@stdlib/random/base/xorshift128/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
<!--

@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+

> A 128-bit xorshift pseudorandom number generator ([PRNG][xorshift]).

<section class="usage">

## Usage

```javascript
var factory = require( '@stdlib/random/base/xorshift128' );
```

#### factory()

Returns a xorshift128+ pseudorandom number generator.

```javascript
var rng = factory();

var r = rng();
// returns <bigint>
```

#### factory.normalized()

Returns a pseudorandom number on the interval `[0,1)`.

```javascript
var rng = factory();

var r = rng.normalized();
// returns <number>
```

#### factory( \[options] )

Returns a xorshift128+ pseudorandom number generator ([PRNG][xorshift]).

```javascript
var rng = factory();
```

The function accepts the following `options`:

- **seed**: pseudorandom number generator seed. Must be a non-negative integer or BigInt. Default: a random seed.
- **state**: a state array (Array) 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 seed is used to seed the returned generator. To seed the generator, provide either a non-negative integer:

```javascript
var BigInt = require( '@stdlib/bigint/ctor' );

var rng = factory({
'seed': BigInt( 1234 )
});

var r = rng();
// returns <bigint>
```

or, for additional control, a BigInt value:

```javascript
var BigInt = require( '@stdlib/bigint/ctor' );

var rng = factory({
'seed': BigInt( 1234 )
});

var r = rng();
// returns <bigint>
```

To return a generator having a specific initial state, set the generator `state` option:

```javascript
var rng1 = factory();

var r;
var i;
for ( i = 0; i < 1000; i++ ) {
r = rng1();
}

var state = rng1.state;

var rng2 = factory({
'state': state
});

var bool = ( rng2() === rng1() );
// returns true
```

#### factory.NAME

The generator name.

```javascript
var rng = factory();

var str = rng.NAME;
// returns 'xorshift128+'
```

#### rng.seed

The value used to seed the generator.

```javascript
var BigInt = require( '@stdlib/bigint/ctor' );

var rng = factory({
'seed': BigInt( 1234 )
});

var seed = rng.seed;
// returns <bigint>
```

#### rng.state

Writable property for getting and setting the generator state.

```javascript
var rng = factory();

var r = rng();
// returns <bigint>

var state = rng.state;
// returns <Array>

r = rng();
// returns <bigint>

rng.state = state;

r = rng();
// returns <bigint>
```

#### rng.toJSON()

Serializes the pseudorandom number generator as a JSON object.

```javascript
var rng = factory();

var o = rng.toJSON();
// returns { 'type': 'PRNG', 'name': 'xorshift128+', 'state': [...], 'params': [] }
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The generator has a period of approximately `2^128` (see [References](#references)).
- Xorshift128+ is a fast, simple pseudorandom number generator with good statistical properties for most applications.
- The generator produces 64-bit pseudorandom integers using BigInt arithmetic.
- The "randomness quality" of the generator's output is suitable for general-purpose use, Monte Carlo simulations, and parallel computations (with different seeds).
- For cryptographic applications, use a cryptographically secure pseudorandom number generator (CSPRNG).
- 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 factory = require( '@stdlib/random/base/xorshift128' );
var BigInt = require( '@stdlib/bigint/ctor' );

var rng = factory({
'seed': BigInt( 1234 )
});

var i;
for ( i = 0; i < 10; i++ ) {
console.log( rng() );
}
```

</section>

<!-- /.examples -->

* * *

<section class="references">

## References

- Vigna, S. (2014). "An experimental exploration of Marsaglia's xorshift generators, scrambled." _ACM Transactions on Mathematical Software (TOMS)_, 42(4), 30. doi:[10.1145/2714064.2714122][vigna:2014].
- Vigna, S. (2018). "Further scramblings of Marsaglia's xorshift generators." _Journal of Computational and Applied Mathematics_, 341, 273–282. doi:[10.1016/j.cam.2018.03.024][vigna:2018].

</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/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>

</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

[vigna:2014]: http://dx.doi.org/10.1145/2714064.2714122

[vigna:2018]: http://dx.doi.org/10.1016/j.cam.2018.03.024

<!-- <related-links> -->

[@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 -->
Loading
Loading