`

haskell - Input and Output - Randomness

阅读更多

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  

 

分享到:
评论

相关推荐

    Atom-ide-haskell-hoogle,在光标下显示符号的滚动信息。对原子的贡献.zip

    Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...

    haskell-mode emacs

    在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...

    haskell-chart, haskell的2D 图表库.zip

    在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...

    Atom-haskell-ghc-mod,哈斯克尔.zip

    **哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...

    Atom-haskell-debug,使用ghci在atom中实现图形haskell调试器.zip

    Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码.zip

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...

    haskell-ghc-mod:haskell-ghc-mod原子包

    从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...

    haskell-ghc-mod:haskell-ghc-mod原子包

    haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...

    Get Programming with HASKELL-2018-英文版.pdf

    Get Programming with HASKELL-2018-英文版

    Atom-atom-haskell-scry,扩散系数.zip

    【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...

    haskell-relational-record-driver-mysql:用于 haskell-relational-record 的 MySQL 驱动程序

    用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...

    Atom-ide-haskell-repl,原子中的ghci repl。对原子的贡献.zip

    Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...

    Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid

    ### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...

    haskell-dev-tools:我用来安装升级与Haskell相关的开发工具的元项目

    在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...

    haskell-dap:Haskell DAP接口数据的实现

    Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...

    haskell-formatter:Haskell源代码格式化程序-Form source code

    haskell-formatter --force --input a.hs --output a.hs hindent a.hs 就地格式化多个文件 hindent a.hs b.hs 将stdin格式化为stdout haskell-formatter hindent 订单进口 完成 hindent --sort-imports … ...

    函数式编程-haskell-to-java

    ### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...

    haskell-tools:Haskell的开发人员工具

    "haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...

    Atom-ide-haskell-cabal,IDE的Cabal后端提供商.zip

    Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。

    haskell-brainfuck:Haskel 脑残翻译

    你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...

Global site tag (gtag.js) - Google Analytics