tiny-prng-wasm
v0.2.5
Published
PRNG in the browser stack
Maintainers
Readme
Tiny PRNG
[!NOTE] Beta release. The quality of generated pseudo random numbers is not tested at now.
This crate provides common psuedo random number generators written in pure Rust, which include:
| name | supported mode | cycle period | reference |
|------------------|---------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Mersenne Twister | MT19937 MT19937_64 | 219937-1 | Saitoh and Matsumoto (1997) |
| Xorshift | xorshift32 xorshift64xorshift128xorshift64*xorshift1024* | 264-1 264-1 2128-1 264-1 21024-1 | Marsaglia (2003), J. Stat. Softw. 8 (14) Vigna (2016), ACM Trans. Math. Softw. Vol. 42 (4), 30 |
| PCG (with LCG) | PCG-XSL-RR-128/64 PCG-XSH-RS-64/32 PCG-XSH-RR-64/32 | 2128 264 264 | O'Neil (2014), HMC-CS-2014-0905Reference implementation |
This library is written in Rust but you can use it from JavaScript via its WASM binary!
For web developers who need to generate many pseudo random numbers, we also provide the npm package tiny-prng-wasm.
Install it as follows:
npm install tiny-prng-wasmAPI
Available constructors
In the npm package, three PRNGs (and one mode for each) are supported:
Pcg(PCG-XSL-RR-128/64)Xorshift64(Xorshift64)Mt64(MT19937_64)
Each of them can be instantiated with its constructor with a seed value:
let generator = new Pcg(1274671829);
console.log(generate.generate());[!WARNING] Be careful to choose a certain randomness source to get a seed for the generator.
These generators are not cryptographically secure random number generators, and a value sequence generated by the one of these generators is perfectly predictable from the set of the algorithm and its seed value.
Available methods
Each generator have identical set of methods for the API. Following operations are supported.
generate
The generate method generates a pseudo random number in unsigned integer.
let g = new Pcg(1826533);
console.log(g.generate());generate_list
The generate_list method generates a list of pseudo random numbers with count
let g = new Pcg(1826533);
let count = 1000;
let list = g.generate_list(count);
console.log(list[100], list[200]);generate_real
The generate_real method generates a pseudo random number in a real number between [0,1].
let g = new Pcg(1826533);
console.log(g.generate_real());generate_real_list
The generate_real_list method generates a list of pseudo random numbers in a real number between [0,1], with count
let g = new Pcg(1826533);
let count = 1000;
let list = g.generate_real_list(count);
console.log(list[100], list[200]);generate_real_ranged
The generate_real_ranged generates a pseudo random number in a real number between specified minimal and maximal
values.
let g = new Pcg(1826533);
console.log(g.generate_real_ranged(0, Math.PI)); // resultsgenerate_real_ranged_list
The generate_real_ranged_list generates a list of pseudo random numbers in a real number between minimal and maximal
values, with specified count.
let g = new Pcg(1826533);
let count = 1000;
let list = g.generate_real_ranged(0, Math.PI, count);
console.log(list[100], list[200]);Benchmarking
Library Routine
Any core routines in the library can generate a pseudo random number within 30 msec. (But you need to built the generator instance with seed before its working.)
See the bench result for 10 million instructions of pseudo random number generation:
user@localhost tiny_prng $ cargo bench | grep -v ignored
# (output omitted...)
running 31 tests
test mt64::tests::bench_mt19937_10mil ... bench: 7,660,233.30 ns/iter (+/- 43,981.22)
test mt::tests::bench_mt19937_32_10mil ... bench: 9,339,750.10 ns/iter (+/- 415,492.15)
test pcg::tests::bench_pcgxshrr6432_10mil ... bench: 9,416,045.80 ns/iter (+/- 589,289.98)
test pcg::tests::bench_pcgxslrr12864_10mil ... bench: 15,627,133.30 ns/iter (+/- 260,361.14)
test xorshift::tests::bench_xorshift1024_10mil ... bench: 23,095,120.80 ns/iter (+/- 7,339,056.80)
test xorshift::tests::bench_xorshift64_10mil ... bench: 18,749,149.90 ns/iter (+/- 186,877.62)Execution environment:
- OS: macOS Sequoia 15.5
- CPU: arm64 (Apple M1)
- Memory: 8GB
[!NOTE] This result is measured with benchmark tests in the library. We are planning further performance evaluations and investigations in the future, in more different execution environments with variety of benchmarking conditions.
Frontend Benchmarking
As the simplest web benchmarking environment, you can try the online benchmarking in your browser.
Simply run make under the wasm_web directory and open http://localhost:8080.
Author
Tiny PRNG Wasm contributors
