在以下程序中,为什么每一行都返回
即使在源代码mkStdGen中也指出:"不同的参数应该能够产生不同的生成器":
True
?我原以为由于我使用不同的种子来初始化mkStdGen
,一些行应该返回True
,而其他行应该返回False
:module Main where
import System.Random
main = do
--why every single line prints "True" ?
print $ fst (random (mkStdGen 1) :: (Bool, StdGen))
print $ fst (random (mkStdGen 2) :: (Bool, StdGen))
print $ fst (random (mkStdGen 3) :: (Bool, StdGen))
print $ fst (random (mkStdGen 4) :: (Bool, StdGen))
print $ fst (random (mkStdGen 5) :: (Bool, StdGen))
print $ fst (random (mkStdGen 6) :: (Bool, StdGen))
print $ fst (random (mkStdGen 7) :: (Bool, StdGen))
print $ fst (random (mkStdGen 8) :: (Bool, StdGen))
print $ fst (random (mkStdGen 9) :: (Bool, StdGen))
print $ fst (random (mkStdGen 10) :: (Bool, StdGen))
即使在源代码mkStdGen中也指出:"不同的参数应该能够产生不同的生成器":
{- |
The function 'mkStdGen' provides an alternative way of producing an initial
generator, by mapping an 'Int' into a generator. Again, distinct arguments
should be likely to produce distinct generators.
-}
mkStdGen :: Int -> StdGen -- why not Integer ?
mkStdGen s = mkStdGen32 $ fromIntegral s
Int
)在第一次运行时会产生一个True
值(length $ takeWhile id $ map fst . random . mkStdGen) [1..]
)。 - bheklilr