mathigon / fermat.js

Mathematics and statistics library for TypeScript.
http://mathigon.io/fermat
MIT License
103 stars 15 forks source link

seeded random numbers? #109

Open ianstormtaylor opened 2 years ago

ianstormtaylor commented 2 years ago

I love the Random helpers in this library, but I'm looking to use them for generative art, and I need a way to create seeded random numbers for consitent, repeatable outputs.

I was wondering if you'd be open to having an extra optional argument added to the functions. For example:

// from...
function bernoulli(p = 0.5) {}

// to...
function bernoulli(p = 0.5, random = Math.random) {}

So for example in p5.js you could do:

let r = Random.bernoulli(0.5, p.random)

Alternatively, the existing random helpers could be refactored to be a class that can be instantiated as well as used as static methods. So you could do:

let random = new Random(p.random)
let r = random.bernoulli()

Which might be nicer.

This would also make it easy to use libraries like seedrandom or seed-random too.