Skip to content

cristian-5/pcg-random-wasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pcg-random-wasm

🎲 Web Assembly PCG random number generator http://www.pcg-random.org .
💗 Handwritten, commented port based on Ph.D. Melissa E. O'Neill's work.

PCG is a family of simple fast space-efficient statistically good algorithms for random number generation.
Unlike many general-purpose RNGs, they are also hard to predict.

This project comes with a p5.js sketch to demonstrate uniform distribuition.
⚠️ Attention: the sketch needs to be served by a web server, otherwise it won't work.

API usage:

WebAssembly.instantiateStreaming(fetch("pcg_prng.wasm")).then(({ instance }) => {
    const seed = instance.exports.pcg32_srandom;
    const biased = instance.exports.pcg32_random_biased;
    const unbiased = instance.exports.pcg32_random_unbiased;
    seed(BigInt(Date.now()), BigInt(Date.now()));
    console.log(biased(20, 30), unbiased(20, 30));
});