alea
alea copied to clipboard
Is this the best syntax possible?
Is the syntax in some parts of alea the best possible?
I'm finding that it's a bit "odd" to have to write, e.g.:
rng.mean(a)
to get the mean of distribution a
, because this makes reference to being a method of rng
, which is conceptually not similar to the expected a.mean()
or mean(a)
.
The reason is that I want to be explicit about the RNG that I use, and possibly switch the one in use. Your syntax can only work with an implicit RNG.
I'm not so experienced with Nim in order to understand how to implement the difference I suggested, but in Python this is done via:
https://docs.python.org/3/library/random.html#random.seed
So I was just thinking that maybe it's possible to do this kind of object in Nim as well.
Perhaps:
https://github.com/python/cpython/blob/3.10/Lib/random.py#L103 ?
What you do in Python is to put a seed for your RNG, which makes the number generation deterministic. But still, you are tied to the default RNG provided by the Python standard library (I think it is a Mersenne twister). If you want to change it, say, to a cryptographically safe RNG, you are out of luck. Moreover, this makes the library inherently thread-unsafe, because RNG generally are mutable, thread unsafe objects, and with the Python design, you have to choose a global one. Making the RNG explicit avoids all these issues