nvzqz/RandomKit

Provide a default random generator?

ldiqual opened this issue · 1 comments

@nvzqz The 4.0.0 release broke all previous client code by adding a new required using: param to all random() methods. While I understand the motivation behind allowing passing a generator, I think it would he sensible to provide a default generator that will fit 99% of cases, and allow people to specify their own generator in case they need something else. I would argue that most people don't need to think about which generator they're using, as long as the randomness is good enough to shuffle an array or give an arbitrary int. I'm not sure what used to be the default generator, but can we please use this from now on?

nvzqz commented

The default generator was previously Xoroshiro. Most random generators out there use the more mature Mersenne Twister algorithm. So you may want to consider that one as well.

The purpose of this change was to make it easier to avoid using a shared mutable state simultaneously when using multiple threads. If this is not a concern, you can pass a reference to the default instance of a generator.

Shuffling example using the default Xoroshiro instance:

var array = [1, 2, 3, 4, 5]
array.shuffle(using: &Xoroshiro.default)

This change also makes functions that take a random generator more modular in the case where you'd like to supply your own random generator outside of what's provided by this library.

Sure, it adds a few more characters (25 in this case) to write, but in my opinion the pros outweigh the cons in not having a default generator.

If a default generator were to be reinstated, the current use of generics would require writing many more separate functions to use the default generator.