xrandom 0.0.1 copy "xrandom: ^0.0.1" to clipboard
xrandom: ^0.0.1 copied to clipboard

outdated

Fast random number generators: xorshift32, xorshift64, xorshift128 and xorshift128+

Generic badge Actions Status Generic badge Generic badge

xrandom #

Fast random number generators in Dart.

Xorshift algorithms are known among the fastest random number generators, requiring very small code and state.

Speed #

Generating 100 million of random numbers with AOT-compiled binary.

Time (lower is better) nextInt nextDouble nextBool
Random (dart:math) 2323 3107 2264
Xorshift32 1269 1930 1467

Simplicity #

It's compatible with the standard Random

import 'package:xrandom/xrandom.dart';

Random xrandom = Xorshift32();

var a = xrandom.nextBool(); 
var b = xrandom.nextDouble();
var c = xrandom.nextInt(n);

Determinism #

Xrandom's classes have a deterministic method. By creating the object like that, you'll get same sequence of numbers every time.

test('my test', () {
    final xrandom = Xorshift32.deterministic();
    // run this test twice ;)
    expect(xrandom.nextInt(1000), 119);
    expect(xrandom.nextInt(1000), 240);
    expect(xrandom.nextInt(1000), 369);    
});    

You can achieve the same determinism by creating the Random with a seed argument. However, this does not protect you from dart:math implementation updates.

In contrast to dart:math, xrandom uses xorshift32, a very specific algorithm. Therefore, the predictability of the Xrandom's deterministic sequences can be relied upon. (but not until the library reaches stable release status)

Compatibility #

Class 64-bit platforms JavaScript
Xorshift32 yes yes
Xorshift128 yes yes
Xoshiro128pp yes yes
Xorshift64 yes no
Xorshift128p yes no

The library has been thoroughly tested to match reference numbers generated by C algorithms. The sources in C are taken directly from scientific publications by George Marsaglia and Sebastiano Vigna, the inventors of the algorithms. The Xorshift128+ results are also matched to reference values from JavaScript xorshift library, that tested the 128+ similarly.

Testing is done in the GitHub Actions cloud on Windows, Ubuntu and macOS in VM and Node.js modes.

Classes #

Class Algorithm Algorithm author Published
Xorshift32 xorshift32 G. Marsaglia 2003
Xorshift64 xorshift64 G. Marsaglia 2003
Xorshift128 xorshift128 G. Marsaglia 2003
Xorshift128p xorshift128+ S. Vigna 2015
Xoshiro128pp xoshiro128++ 1.0 D. Blackman and S. Vigna 2019

Speed optimizations #

nextFloat, unlike nextDouble, does not seek to create the highest quality 64-bit floating-point number. This method prefers speed to accuracy. It transforms a single 32-bit integer into a double. Therefore, the result is limited to a maximum of 2^32-1 values.

Time (lower is better) nextDouble nextFloat
Random (dart:math) 3107 -
Xorshift32 1930 696
Xorshift64 2164 1340
Xorshift128 3164 1293
Xorshift128p 3023 1373
Xoshiro128pp 4535 2086

The nextInt32() and nextInt64() do not accept any arguments. They return the raw output of the RNGs.

var xrandom = Xorshift128p(); 
xrandom.nextInt32();  // 32-bit unsigned 
xrandom.nextInt64();  // 64-bit signed
Time (lower is better) nextInt nextInt32 nextInt64
Random (dart:math) 2323 - -
Xorshift32 1269 767 -
Xorshift64 1974 1292 1381
Xorshift128 1967 1301 -
Xorshift128p 2015 1364 1521
Xoshiro128pp 2546 2048 -

More benchmarks #

Time (lower is better) nextInt nextDouble nextBool
Random (dart:math) 2323 3107 2264
Xorshift32 1269 1930 1467
Xorshift64 1974 2164 1393
Xorshift128 1967 3164 1479
Xorshift128p 2015 3023 1413
Xoshiro128pp 2546 4535 1500

All the benchmarks on this page are from AOT-compiled binaries running on AMD A9-9420e with Ubuntu 20.04. Time is measured in milliseconds.

11
likes
0
pub points
77%
popularity

Publisher

verified publisherrevercode.com

Fast random number generators: xorshift32, xorshift64, xorshift128 and xorshift128+

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on xrandom