Search code examples
haskellrandomquickcheck

SIMPLE random number generation


I'm writing this after a good while of frustrating research, and I'm hoping someone here can enlighten me about the topic.

I want to generate a simple random number in a haskell function, but alas, this seems impossible to do without all sorts of non-trivial elements, such as Monads, asignation in "do", creating generators, etc.

Ideally I was looking for an equivalent of C's "rand()". But after much searching I'm pretty convinced there is no such thing, because of how the language is designed. (If there is, please someone enlighten me). As that doesn't seem feasible, I'd like to find a way to get a random number for my particular problem, and a general explanation on how it works to get a random number.

prefixGenerator :: (Ord a, Arbitrary a) => Gen ([a],[a])
prefixGenerator = frequency [ 
    (1, return ([],[])),
    (2, do {
            xs1 <- orderedListEj13 ;
            xs2 <- orderedListEj13 ;
            return (xs1,xs2)
       }),
    (2, do {                
            xs2 <- orderedListEj13 ;
            return ((take RANDOMNUMBERHERE xs2),xs2)
       })
    ]

I'm trying to get to grips with QuickCheck but my inability to use random numbers is making it hard. I've tried something like this (by putting an drawInt 0 (length xs2) instead of RANDOMNUMBERHERE)but I get stuck with the fact that take requires an Int and that method leaves me with a IO Int, which seems impossible to transform to an Int according to this.


Solution

  • As haskell is a pure functional programming language, functions are referentially transparent which means essentially that only a function's arguments determine its result. If you were able to pull a random number out of the air, you can imagine how that would cause problems.

    I suppose you need something like this:

    prefixGenerator :: (Ord a, Arbitrary a) => Gen ([a],[a])
    prefixGenerator = do
      randn <- choose (1,999) -- number in range 1-999
      frequency [ 
        (1, return ([],[])),
        (2, do {
                xs1 <- orderedListEj13 ;
                xs2 <- orderedListEj13 ;
                return (xs1,xs2)
           }),
        (2, do {                
                xs2 <- orderedListEj13 ;
                return ((take randn xs2),xs2)
           })
        ]
    

    In general in haskell you approach random number generation by either pulling some randomness from the IO monad, or by maintaining a PRNG that is initialized with some integer seed hard-coded, or pulled from IO (gspr's comment is excellent).

    Reading about how pseudo random number generators work might help you understand System.Random, and this might help as well (scroll down to section on randomness).