<p>There’s lots of random and similar sounding words in this episode. I hope you can still follow what I’m trying to explain, but I’m aware that it might be hard.</p>
<p>Haskell functions are pure, meaning that they will always produce same values for same set of arguments. This might sound hard when you want to generate random numbers, but it turns out that the solution isn’t too tricky.</p>
<p>First part to the puzzle is type class <code>RandomGen</code>:</p>
<pre><code>class RandomGen g where
    next :: g -&gt; (Int, g)
    genRange :: g -&gt; (Int, Int)
    split :: g -&gt; (g, g)</code></pre>
<p><code>next</code> produces tuple, where first element is random <code>Int</code> and second element is new random generator. <code>genRange</code> returns tuple defining minimum and maximum values this generator will return. <code>split</code> produces tuple with two new random generators.</p>
<p>Using <code>RandomGen</code> to produce random values of specific type or for specific range requires a bit of arithmetic. It’s easier to use <code>Random</code> that defines functions for that specific task:</p>
<pre><code>class Random a where
    randomR :: RandomGen g =&gt; (a, a) -&gt; g -&gt; (a, g)
    random :: RandomGen g =&gt; g -&gt; (a, g)
    randomRs :: RandomGen g =&gt; (a, a) -&gt; g -&gt; [a]
    randoms :: RandomGen g =&gt; g -&gt; [a]
    randomRIO :: (a, a) -&gt; IO a
    randomIO :: IO a</code></pre>
<ul>
<li><code>randomR</code>, when given range and random generator, produces tuple with random number and new generator</li>
<li><code>random</code>, is similar but doesn’t take range. Instead it will use minimum and maximum specific to that data type</li>
<li><code>randomRs</code>, takes range and produces infinite list of random values within that range</li>
<li><code>randoms</code>, simply produces infinite list of random values using range that is specific to datatype</li>
<li><code>randomRIO</code> and <code>randomIO</code> are effectful versions that don’t need random generator, but use some default one</li>
</ul>
<p>In short, <code>RandomGen</code> is source of randomness and <code>Random</code> is datatype specific way of generating random values using random generator <code>RandomGen</code>.</p>
<p>Final part of the puzzle is where to get <code>RandomGen</code>? One could initialize one manually, but then it wouldn’t be random. However, there’s function <code>getStdGen</code> that will seed <code>RandomGen</code> using OS default random number generator, current time or some other method. Since it has signature of <code>getStdGen :: IO StdGen</code>, one can only call it in IO monad.</p>
<p>Functions that operate with IO can only be called from other IO functions. They can call pure functions, but pure functions can’t call them. So there’s two options: have the code that needs random numbers in effectful function or get <code>RandomGen</code> in effectful function and pass it to pure function.</p>
<h2 id="example">Example</h2>
<pre><code>import System.Random
import Data.List

-- | get n unique entries from given list in random order
-- | if n &gt; length of list, all items of the list will be returned
getR :: RandomGen g =&gt; g -&gt; Int -&gt; [a] -&gt; [a]
getR g n xs =
    fmap (xs !!) ids
    where
        ids = take (min n $ length xs) $ nub $ randomRs (0, length xs - 1) g

-- | Returns 4 unique numbers between 1 and 10 (inclusive)
test :: IO [Int]
test = do
    g &lt;- getStdGen
    return $ getR g 4 [1..10]</code></pre>
<h2 id="in-closing">In closing</h2>
<p>Pseudo randomness doesn’t require IO, only seeding the generator does. Simple computation that don’t require many calls to <code>random</code> are easy enough. If you need lots of random values, <a href="https://hackage.haskell.org/package/MonadRandom-0.4.1/docs/Control-Monad-Random-Class.html"><code>MonadRandom</code></a> is better suited. It takes care of carrying implicit <code>RandomGen</code> along while your computation progresses.</p>
<p>Best way to catch me nowadays is either email or fediverse where I’m <code>Tuula@mastodon.social</code></p>
