[Haskell-beginners] What to use when you need random values?

David McBride toad3k at gmail.com
Fri Jun 23 15:10:05 UTC 2017


When you are unsure about the differences between functions, it can be
good to read the haddocks for the library.

http://hackage.haskell.org/package/random-1.1/docs/System-Random.html

The standard haskell random library supports the idea of splitting a
seed randomly.  You take one seed and split it, and now you  have two
seeds, which will each generate different randoms independently.
getStdGen gets the current global seed.  newStdGen splits new a seed
off of the current global seed.  mkStdGen allows you to create a seed
from a value so that you can get the same set of randoms repeatedly.

I would say if you are in IO, just use randomRIO.  If you are in
monadic code that not IO at its base, you should use MonadRandom
library on hackage.  Quickcheck randomness is only really used in
quickcheck, although it is probably based off the standard libraries.

Just keep in mind that randomness is a concept that is a little hard
to wrap your head around in haskell until you've been using it a
little while.

On Fri, Jun 23, 2017 at 10:24 AM, Silent Leaf <silent.leaf0 at gmail.com> wrote:
> Hi,
> I've had trouble finding the best way(s) to use random values in haskell, as
> it seems like there are several modules that either do the same thing or
> reuse one another i'm not sure.
>
> There is System.Random
> - is it better to use the streams random(R)s or a more imperative randomRIO?
> - is it better to use mkStdGen or newStdGen or getStdGen?
> There is Test.QuickCheck and its type(class?) Gen
> There is a module in Control.Monad (i think) which exports the type Rnd
>
> What about performances, and all those options? What do you like to use with
> random numbers?
>
> I know that's a lot of questions. feel free to only answer to a few of them.
> :)
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>


More information about the Beginners mailing list