Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.Random behaving very strangely; numbers displayed are not the numbers being operated on
    text
    copied!<p>I needed to generate some random numbers for testing purposes. I have never used Data.Random before. I needed to do many tests and I would not know the amount of numbers I would need in advance, but I would know that they should fall into a certain bound. I wrote the function:</p> <pre><code>import System.Random f k = (\(x,y) -&gt; x*y &lt; (10^k) &amp;&amp; x*y &gt;0 ) rands n g k = take n $ filter (\(x,y) -&gt; x*y &lt; (10^k) &amp;&amp; x*y &gt;0 ) (zip a b) where a = randomRs (1::Int, 10^k-1) ga b = randomRs (1::Int, 10^k-1) gb (ga, gb) = split $ mkStdGen g </code></pre> <p>and began to test things using the numbers it generated. I began to notice strange things when using this function:</p> <pre><code>testRands n k = do g1 &lt;- randomIO g2 &lt;- randomIO let r = rands n (mkStdGen g1, mkStdGen g2) k putStrLn $ show r putStrLn $ show $ map (\(x,y) -&gt; x*y &lt; 10^k &amp;&amp; x*y &gt;0) r putStrLn $ show $ map (\(x,y) -&gt; x*y) r putStrLn $ show $ maximum $ map (\(x,y) -&gt; x*y) r *Main&gt; testRands 4 7 [(6193574,9385226),(9634973,5475375),(5070277,7408626),(3801396,5652588)] [True,True,True,True] [4353660,6993107,2038586,4030960] 6993107 </code></pre> <p>I then copy the numbers it generated and paste them back into GHCI and call the exact same predicate function on all of them:</p> <pre><code>*Main&gt; map (f 7) [(6193574,9385226),(9634973,5475375),(5070277,7408626),(3801396,5652588)] [False,False,False,False] </code></pre> <p>This makes no sense and I don't even know where to start trying to fix it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload