The package provides multiple pseudo-random number generators.
Note: The PRNGs generate statistical pseudo-random numbers. They are not cryptographically secure.
Currently implemented generators:
The package is published on Mops and GitHub. Please refer to the README on GitHub where it renders properly with formulas and tables.
API documentation: here on Mops
For updates, help, questions, feedback and other requests related to this package join us on:
You need mops installed. In your project directory run:
mops add prngIn the Motoko source file import the generators you need:
import { Seiran128; SFC64; SFC32 } "mo:prng";
(Importing the modules directly — e.g. import Seiran128 "mo:prng/Seiran128" — works too and is required for the rng.next() method-call syntax to resolve.)
The two most commonly used generators from this package are Seiran128 and SFC64a. They both produce Nat64 output values. SFC64a is compatible with numpy.
import { Seiran128; SFC64 } "mo:prng";
let seed : Nat64 = 0;
let rng = Seiran128.new(seed);
let seq : [Nat64] = [rng.next(), rng.next()];
let rng2 = SFC64.SFC64a(seed);
let seq2 : [Nat64] = [rng2.next(), rng2.next()];
The seed argument is optional; omitting it uses each algorithm's default seed:
import { Seiran128; SFC64 } "mo:prng";
let rng = Seiran128.new(); // uses defaultSeiran128Seed
let rng2 = SFC64.SFC64a(); // uses defaultSFC64Seed
There are also two recommended Nat32 generators, SFC32a and SFC32b, used as follows.
import { SFC32 } "mo:prng";
let seed : Nat32 = 0;
let rng = SFC32.SFC32a(seed); // or SFC32.SFC32b(seed)
let seq : [Nat32] = [rng.next(), rng.next()];
For SFC the internal parameters of the generator can also be customized with a constructor like SFC64.new(24, 11, 3, seed).
For more details take a look at the test files, the documentation in the source code, or https://mops.one/prng/docs.
Run:
git clone git@github.com:research-ag/prng.git
cd prng
mops testTo format the code, run:
npx -y prettier --plugin prettier-plugin-motoko --write '**/*.{mo,json,md}'Run
mops benchThe benchmarks were produced with mops bench --replica dfx.
Wasm instructions per invocation of next().
| method | Seiran128 | SFC64 | SFC32 |
|---|---|---|---|
| next | 382 | 754 | 380 |
Heap allocation per invocation of next().
| method | Seiran128 | SFC64 | SFC32 |
|---|---|---|---|
| next | 12 | 12 | 4 |
MR Research AG, 2023-26
Main author: Timo Hanke (timohanke)
Contributors: Andy Gura (AndyGura), react0r-com
Apache-2.0