alea icon indicating copy to clipboard operation
alea copied to clipboard

Is this the best syntax possible?

Open mavavilj opened this issue 2 years ago • 3 comments

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).

mavavilj avatar May 05 '22 14:05 mavavilj

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.

andreaferretti avatar May 05 '22 16:05 andreaferretti

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 ?

mavavilj avatar May 05 '22 17:05 mavavilj

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

andreaferretti avatar May 05 '22 17:05 andreaferretti