快速检查卡生成器

时间:2018-08-16 22:55:42

标签: haskell quickcheck

好,所以我砍了这个:

prop = forAll genCards $ \cards -> collect (go cards == True) $ isFlush cards == go cards

go cards = (length . nub .  map (\w -> last w)) cards == 1

genCard :: Gen String
genCard = elements[ "1C", "2C", "3C", "4C", "5C", "6C", "7C", "9C", "KC", "QC", "JC", "1H", "2H", "3H", "4H", "5H", "6H", "7H", "9H", "KH", "QH", "JH", "1S", "2S", "3S", "4S", "5S", "6S", "7S", "9S", "KS", "QS", "JS", "1D", "2D", "3D", "4D", "5D", "6D", "7D", "9D", "KD", "QD", "JD"] 

genCards :: Gen [String]
genCards = do
  replicateM 5 genCard

但是,必须有一个更好的方法,但是我不知道如何结合一个数字生成器+一个西服生成器,以及一种确保产生更多同花顺的方法(根据随机几率约为0.4%收集)。

1 个答案:

答案 0 :(得分:4)

要生成卡片:

genVal = elements "123456789JQK"
genSuit = elements "CHSD"

genCard = do
    val <- genVal
    suit <- genSuit
    return [val, suit]

要生成冲洗:

genFlush = do
    vals <- replicateM 5 genVal
    suit <- genSuit
    return [[val, suit] | val <- vals]

我的genFlush与您的genCards一样,并不保证以此方式生成的卡是不同的。您可以使用frequency选择给定概率与普通手比较同花;例如冲洗60%:

genHandThatIsProbablyAFlush = frequency
    [ (3, genFlush)
    , (2, genCards)
    ]