zoo
zoo

Reputation: 1911

Haskell random number generation

What's the best way to handle random number generation in Haskell (or what are the tradeoffs)?

I haven't really seen an authoritative answer.

Consider: minimizing the impact on otherwise pure functions, how / when to seed, performance, thread safety

Upvotes: 5

Views: 956

Answers (1)

fuz
fuz

Reputation: 93014

IMHO, the best idea is to keep the generator in a strict state record. Then you can use the ordinary do-Syntax to work with the generator. Seeding is done only once - at the beginning of the main program (or at the beginning of each thread). You can avoid IO by using the split operation, which yields two random generators from one. (Different, of course).

As state is still pure, threadsafety can be guaranteed. Additionally, you can always escape state by giving a random generator to the function. This is useful for instance in case of automatic unit tests.

Upvotes: 3

Related Questions