jeudi 25 mai 2017

Haskell `randoms` function not behaving well with my library

I'm trying to write a Haskell library for cryptographically secure random numbers. The code follows:

module URandom (URandom, initialize) where

import qualified Data.ByteString.Lazy as B
import System.Random
import Data.Word

newtype URandom = URandom [Word8]

instance RandomGen URandom where
  next (URandom (x : xs)) = (fromIntegral x, URandom xs)
  split (URandom l) = (URandom (evens l), URandom (odds l))
    where evens (x : _ : xs) = x : evens xs
          odds (_ : x : xs) = x : odds xs
  genRange _ = (fromIntegral (minBound :: Word8), fromIntegral (maxBound :: Word8))

initialize :: IO URandom
initialize = URandom . B.unpack <$> B.readFile "/dev/urandom"

Unfortunately, it's not behaving like I want. In particular, performing

take 10 . randoms <$> initialize

yields (something similar to)

[-4611651379516519433,-4611644973572935887,-31514321567846,9223361179177989878,-4611732094835278236,9223327886739677537,4611709625714976418,37194416358963,4611669560113361421,-4611645373004878170,-9223329383535098640,4611675323959360258,-27021785867556,9223330964083681227,4611705212636167666]

which to my, albiet untrained, eye, does not appear very random. A lot of 46... and 92... in there.

What could be going wrong? Why doesn't this produce well-distributed numbers? It's worth noting that even if I concatenate together Word8s to form Ints the distribution does not improve, I didn't think it was worth including that code here.




Aucun commentaire:

Enregistrer un commentaire