我的问题很简单,我想在我的haskell代码中有一个功能如下:我调用它,它返回一个随机整数。我用它来根据生成的数字生成四联蛋白,有什么想法吗?
编辑,这是我现在执行代码的方式,现在看起来 该代码将类似于:
Point是我自己创建的数据结构,它并不重要。 使用unsafePerform的问题是每次都会重复相同的数字
getRandomInt :: Int
getRandomInt = unsafePerformIO (getStdRandom (randomR (1, 7)))
getRandomTetromino :: [Point]
getRandomTetromino = getRT getRandomInt
getRT :: Int -> [Point]
getRT c
| c == 1 = [ Point 5 1 "T", Point 5 0 "T", Point 6 1 "T", Point 4 1 "T"]
| c == 2 = [ Point 5 1 "Z", Point 5 0 "Z", Point 4 1 "Z", Point 4 2 "Z"]
| c == 3 = [ Point 5 1 "I", Point 5 0 "I", Point 5 2 "I", Point 5 3 "I"]
| c == 4 = [ Point 5 1 "L", Point 6 1 "L", Point 4 1 "L", Point 6 0 "L"]
| c == 5 = [ Point 5 1 "J", Point 5 0 "J", Point 6 0 "J", Point 5 2 "J"]
| c == 6 = [ Point 5 1 "S", Point 5 0 "S", Point 6 0 "S", Point 4 1 "S"]
| c == 7 = [ Point 5 0 "O", Point 6 0 "O", Point 6 1 "O", Point 5 1 "O"]
答案 0 :(得分:1)
您可以使用以下方法做到这一点:
import System.Random
import Control.Monad (replicateM)
main = replicateM 10 (randomIO :: IO Int) >>= print