Haskell random number generation

What is the best way to handle random number generation in Haskell (or what trade-offs)?

I really did not see an authoritative answer.

Consider: minimizing the effect on purely clean functions like / when seeds, productivity, thread safety

+5
source share
1 answer

IMHO, the best idea is to keep the generator in a strict state record . Then you can use regular do-Syntax to work with the generator. Sowing is performed only once - at the beginning of the main program (or at the beginning of each thread). You can avoid I / O by using an operation splitthat gives two random generators from one. (Different, of course).

Since the condition is still clean, thread safety can be guaranteed. In addition, you can always escape the state by providing a random function generator. This is useful, for example, in the case of automatic unit tests.

+3
source

All Articles