I use Test.QuickCheck to generate random Sudoku puzzles.
data Sudoku = Sudoku { getSudoku :: [[Maybe Int]] } deriving (Show, Eq)
rows :: Sudoku -> [[Maybe Int]]
rows (Sudoku rs) = rs
--B1
printSudoku :: Sudoku -> IO ()
printSudoku s = do
putStrLn . unlines . map (map (maybe '.' (head . show))) $ rows s
--C1
cell :: Gen (Maybe Int)
cell = suchThatMaybe (frequency [(90, choose (0,0)),(10, choose(1,9))]) (/=0)
--C2
instance Arbitrary Sudoku where
arbitrary = do
rows <- sequence [ sequence [ cell | j <- [1..9] ] | i <- [1..9] ]
return (Sudoku rows)
--C3
prop_Sudoku :: Sudoku -> Bool
prop_Sudoku = isSudoku
checkRandomSudoku :: IO [Bool]
checkRandomSudoku = do
s <- sample' (arbitrary :: Gen Sudoku)
return $ map isSudoku s
The code is working fine. However, when I perform
a <- sample '(arbitrary :: Gen Sudoku)
sequence $ map printSudoku a
it returns something like this:
....5..3.
...4.....
...2.....
...5.....
.........
...33....
...5.....
...2.4...
.........
.........
.343.....
.........
......9.2
.........
45....5.1
.2......7
.7..88.34
.9....6..
....2..8.
.2121638.
.7.7...9.
4..45.6..
.....6.2.
..6.6....
53..9.6..
..9....7.
.47892...
.373411..
5...3282.
...45..9.
8989..18.
31.8113..
9..35.6..
4.685....
.4....39.
7..6.5.76
48.178.53
1.871.4.4
3165.17..
.1...7.59
.98126.51
6.6...775
9.4636952
.5..239..
372.....8
.34.73129
.5.8.27.1
344.34931
28.6.94.1
6327.3..8
3743.5496
93...7984
..82.8...
..3.54.93
273847853
5568.7465
832.73515
3766..6.7
.7.196256
1.96.9.3.
.7156.268
1615.196.
.392..633
731652284
863.8.768
31..5.5.6
961.5.467
1245.1159
5..275471
52.727759
6.656.849
99.72352.
which is obviously not random. The distribution of empty cells is initially very high, and then slowly decreases. Am I using the wrong function or the wrong? Thanks
source
share