n most other programming languages, you have functions that give you back some random number. Each time you call that function, you get back a (hopefully) different random number. How about Haskell? Well, remember, Haskell is a pure functional language. What that means is that it has referential transparency. What THAT means is that a function, if given the same parameters twice, must produce the same result twice.
You need to import System.Random module. where it has a random function. the type is
random :: (RandomGen g, Random a) => g -> (a, g)
The RandomGen typeclass is for types that can act as sources of randomness. The Random typeclass is for things that can take on random values. A boolean value can take on a random value, namely True or False. A number can also take up a plethora of different random values. Can a function take on a random value? I don't think so, probably not! If we try to translate the type declaration of random to English, we get something like: it takes a random generator (that's our source of randomness) and returns a random value and a new random generator. Why does it also return a new generator as well as a random value?
To use our random function, we have to get our hands on one of those random generators. The System.Random module exports a cool type, namely StdGen that is an instance of the RandomGen typeclass. We can either make a StdGenmanually or we can tell the system to give us one based on a multitude of sort of random stuff.
To manually make a random generator, use the mkStdGen function. It has a type of mkStdGen :: Int -> StdGen. It takes an integer and based on that, gives us a random generator. Okay then, let's try using random and mkStdGen in tandem to get a (hardly random) number.
But we cannnot just do like this:
ghci> random (mkStdGen 100)
you will not be able to get the random value as you expected, the result we might get is like this:
<interactive>:1:0: Ambiguous type variable `a' in the constraint: `Random a' arising from a use of `random' at <interactive>:1:0-20 Probable fix: add a type signature that fixes these type variable(s)
the reason is that random can return a val eof any type of the Random typeclass. We have to tell haskell what kind of type that we want.
so this is the right way.
ghci> random (mkStdGen 100) :: (Int, StdGen) (-1352021624,651872571 1655838864)
So what is returned is the random number and the second is the text representation of the new random generator.
In this section, we will examine
- the random value and the random generator
- the Random typeclass and the RandomGen typeclass
- random and the usetips of the random value (mkStdGen)
- randoms and a simulated implementation with recusive random calls
- randomRs which return a ranged random
- how to get a true random - getStdGen (IO StdGen) which bind to global random
- some quirks dealing with getStdGen and some means to simulate true random
- getStdGen and the newStdGen
- we will make a guess game.
We have covered that random function wll return a random value and new RandomGen.
as we have said before, the typeclasses returns inludes Random and RandomGen.
Now the example below will show the following.
- threeCoins
- random' and infiniteRandom
- randoms, randomR, randomRs
-- file : -- randomness_io101.hs -- description: -- this is a file which will demonstrate the use of randomness functions import System.Random -- random :: (RandomGen g, Random a) => g -> (a, g). random :: (RandomGen g, Random a) => g -> (a, g). -- to get an standard random generator, you can use the "mkStdGen" function as below -- -- random (mkStdGen 100) -- ghci> random (mkStdGen 100) :: (Int, StdGen) -- (-1352021624,651872571 1655838864) -- -- -- and if we give the same Random generator -- ghci> random (mkStdGen 100) :: (Int, StdGen) -- (-1352021624,651872571 1655838864) -- constue on the return result of the textual representation of the randomness -- the first component of the tuple is our number whereas the second component is a textual representation of our new random generator. -- more on this random generator -- ghci> random (mkStdGen 949488) :: (Float, StdGen) -- (0.8938442,1597344447 1655838864) -- ghci> random (mkStdGen 949488) :: (Bool, StdGen) -- (False,1485632275 40692) -- ghci> random (mkStdGen 949488) :: (Integer, StdGen) -- (1691547873,1597344447 1655838864) threeCoins :: StdGen -> (Bool, Bool, Bool) threeCoins gen = let (firstCoin, newGen) = random gen (secondCoin, newGen') = random newGen (thirdCoin, newGen'') = random newGen' in (firstCoin, secondCoin, thirdCoin) -- ghci> threeCoins (mkStdGen 21) -- (True,True,True) -- ghci> threeCoins (mkStdGen 22) -- (True,False,True) -- ghci> threeCoins (mkStdGen 943) -- (True,False,True) -- ghci> threeCoins (mkStdGen 944) -- (True,True,True) -- : randoms -- return a infinite set of randoms -- ghci> take 5 $ randoms (mkStdGen 11) :: [Int] -- [-1807975507,545074951,-1015194702,-1622477312,-502893664] -- ghci> take 5 $ randoms (mkStdGen 11) :: [Bool] -- [True,True,True,True,False] -- ghci> take 5 $ randoms (mkStdGen 11) :: [Float] -- [7.904789e-2,0.62691015,0.26363158,0.12223756,0.38291094] -- suppose we are going to make a new infinite sequence of random number -- a recursive implementation is like this: randoms' :: (RandomGen g, Random a) => g -> [a] randoms' gen = let (value, newGen) = random gen in value:randoms' newGen -- g, a and n are like alias to the typeclass of -- RandomGen, Random and Num finiteRandoms :: (RandomGen g, Random a, Num n) => n -> g -> ([a], g) finiteRandoms 0 gen = ([], gen) finiteRandoms n gen = let (value, newGen) = random gen (restOfList, finalGen) = finiteRandoms(n - 1) newGen in (value: restOfList, finalGen) -- call with the following methods -- finiteRandoms 5 (mkStdGen 11) :: ([Int], StdGen) -- *Main System.Random> finiteRandoms 5 (mkStdGen 11) :: ([Int], StdGen) -- ([5258698905265467991,-1046130112415077602,3603401487739301952,-595625523242114439,-242088768969841391],912247095 2118231989) -- randomR (1, 6) (mkStdGen 359353) -- ghci> randomR (1,6) (mkStdGen 359353) -- (6,1494289578 40692) -- ghci> randomR (1,6) (mkStdGen 35935335) -- (3,1250031057 40692) -- take 10 $ randomRs ('a','z') (mkStdGen 3) :: [Char]
as you know that random is implemented as pure function, which means there is no way to get a true random number, but there exist a function of mkStdGen which has the type of IO StdGen . here is how you use it.
-- file : -- randomstring_gen.hs -- description: -- generate the random string with the import System.Random main = do gen <- getStdGen putStr $ take 20 (randomRs ('a','z') gen) -- what if you run it like this? calling getStdGen again, do we get a new Gen? -- no, we DON'T main2 = do gen <- getStdGen putStr $ take 20 (randomRs ('a', 'z') gen) gen2 <- getStdGen putStr $ take 20 (randomRs ('a', 'z') gen2)
and we saw that if you bind getStdGen again, you wll find the two sequence will yield the same result.
so what to do ? you can leverage getStdGen together with newStdGen, which will update the global Random generator and encapsulate anothe one as it results.
the following example will show how this works out and we in the comment will also introduce some workaround which will actually smartly generate two random sequences.
-- file : -- randomstring2.hs -- description: -- generate random string 2 import System.Random -- this will always generate the same result, import System.Random import Data.List -- use an infnite generator sequence and split at the 20 parts -- main = do -- gen <- getStdGen -- let randomChars = randomRs ('a','z') gen -- (first20, rest) = splitAt 20 randomChars -- (second20, _) = splitAt 20 rest -- putStrLn first20 -- putStr second20 -- main = do -- gen <- getStdGen -- putStrLn $ take 20 (randomRs ('a','z') gen) -- gen2 <- getStdGen -- putStr $ take 20 (randomRs ('a','z') gen2) -- or you can do this -- newStdGen -- which splits our current random generator into two generators.. it updates the global generator with one of them and encapsulate -- the other as its result -- Another way is to use the newStdGen action, which splits our current random generator into two generators. It updates the global random generator with one of them and encapsulates the other as its result. main = do gen <- getStdGen putStrLn $ take 20 (randomRs ('a', 'z') gen) gen' <- newStdGen putStr $ take 20 (randomRs ('a', 'z') gen')
as promised, we will present you with the guess name that we long has invented.
-- file : -- make the user to guess which number it is thinking of -- description: -- generate random string 2 import System.Random import Control.Monad(when) main = do gen <- getStdGen askForNumber gen askForNumber :: StdGen -> IO () askForNumber gen = do let (randNumber, newGen) = randomR (1,10) gen :: (Int, StdGen) putStr "Which number in the range from 1 to 10 am I thinking of? " numberString <- getLine when (not $ null numberString) $ do let number = read numberString if randNumber == number then putStrLn "You are correct!" else putStrLn $ "Sorry, it was " ++ show randNumber askForNumber newGen -- you can do this way : -- -- import System.Random -- import Control.Monad(when) -- -- main = do -- gen <- getStdGen -- let (randNumber, _) = randomR (1,10) gen :: (Int, StdGen) -- putStr "Which number in the range from 1 to 10 am I thinking of? " -- numberString <- getLine -- when (not $ null numberString) $ do -- let number = read numberString -- if randNumber == number -- then putStrLn "You are correct!" -- else putStrLn $ "Sorry, it was " ++ show randNumber -- newStdGen -- main