我正在嘗試創建串列的隨機排列。我是函式式語言中的隨機性新手,還沒有完全掌握 monad,但我以我認為應該有效的方式使用了 Random.newStdGen 和 Random.Shuffle.shuffle'。
我遇到的問題是,我得到了很多重復的排列,以至于我似乎錯誤地使用或錯誤地理解了生成器的拆分函式。
相關功能在這里:
doGenerateInput :: [[Int]] -> System.Random.StdGen -> Int -> Int -> [[Int]]
doGenerateInput acc gen n 0 = acc
doGenerateInput acc gen n k =
doGenerateInput
(System.Random.Shuffle.shuffle' [1 .. n] n gen : acc) accumulator
(fst (System.Random.split gen))
n
(k -1)
generateInput :: Int -> Int -> IO [[Int]]
generateInput n k = do
gen <- System.Random.newStdGen
return (doGenerateInput [] gen n k)
這里generateInput應該創建k的隨機排列[1..n]。它在將生成器傳遞到下一個遞回級別之前對其進行拆分,因此每個排列在統計上應該彼此無關。然而,我得到的實際結果包括大量重復。通常連續兩次回傳相同的排列。有時甚至連續三次。有人對我可能做錯的事情有任何建議嗎?
這是我從它那里收到的輸出。
8 18 1 6 17 19 7 9 15 2 11 12 20 16 10 5 4 14 13 3
12 11 8 20 1 6 19 7 9 17 2 13 14 18 10 5 4 16 15 3
3 13 12 9 1 2 8 10 11 19 4 15 16 20 14 7 6 18 17 5
3 13 12 9 1 2 8 10 11 19 4 15 16 20 14 7 6 18 17 5
7 8 3 15 14 16 20 11 1 2 10 12 13 5 4 19 6 18 17 9
7 8 3 15 14 16 20 11 1 2 10 12 13 5 4 19 6 18 17 9
7 8 3 15 14 16 20 11 1 2 10 12 13 5 4 19 6 18 17 9
7 8 3 15 14 16 20 11 1 2 10 12 13 5 4 19 6 18 17 9
8 6 9 10 7 11 3 19 18 20 15 1 2 14 17 16 4 12 5 13
18 8 6 9 10 7 11 3 20 19 15 1 2 14 17 16 4 12 5 13
8 11 5 13 15 4 20 18 16 14 10 3 12 1 19 7 9 6 2 17
16 8 11 5 13 6 15 18 4 19 17 12 3 14 1 9 10 7 2 20
2 17 9 12 6 14 7 16 19 5 20 18 13 4 15 1 8 11 3 10
7 2 18 10 6 14 8 16 9 19 5 20 15 4 17 1 11 13 3 12
17 7 2 19 13 10 6 15 8 18 9 5 14 11 16 3 1 20 4 12
11 4 19 8 2 6 16 13 9 18 10 12 20 3 7 15 5 1 17 14
17 11 4 20 13 10 8 2 6 19 15 9 3 14 5 18 16 7 1 12
17 11 4 20 13 10 8 2 6 19 15 9 3 14 5 18 16 7 1 12
12 18 11 4 1 15 13 9 3 7 17 10 5 16 6 20 19 8 2 14
12 18 11 4 1 15 13 9 3 7 17 10 5 16 6 20 19 8 2 14
根據 的檔案split,我希望從 split 回傳的兩個生成器不相關。但是重復率似乎表明生成一個亂數fst (split gen)產生的結果與gen大約一半的時間相同。
Returns two distinct pseudo-random number generators. Implementations should take care to ensure that the resulting generators are not correlated.
https://hackage.haskell.org/package/random-1.2.1/docs/System-Random.html#v:split
I have found a solution, but I don't understand why it works
If I use snd (split gen) instead of fst (split gen), I do not get any repeats. However based on the documentation, I'm unsure of why. It doesn't make any notes of a difference between the first and second generators returned.
Any insight would be appreciated.
uj5u.com熱心網友回復:
在設計中存在某種假設,random即一個RandomGen值只使用一次。當您重復使用它們時,可能會發生奇怪的事情。我不知道如何實施RandomGen'ssplit來給你這個結果,但我可以告訴你這是假設你不會做你正在做的事情。傳遞gen給兩者split并shuffle'使用它兩次。用于您的用例的預期方法split是預先呼叫 split,然后將其回傳值之一傳遞shuffle'給遞回呼叫,另一個傳遞給遞回呼叫。
uj5u.com熱心網友回復:
您不需要使用split,這通常只在樹狀結構中需要。
您可以像這樣生成排列:
$ ghci
GHCi, version 8.8.4: https://www.haskell.org/ghc/ :? for help
...
λ>
λ> import System.Random
λ> import Control.Monad
λ> import Control.Monad.Random
λ> import System.Random.Shuffle
λ>
λ> :type replicateM
replicateM :: Applicative m => Int -> m a -> m [a]
λ>
λ> :type shuffleM
shuffleM :: MonadRandom m => [a] -> m [a]
λ>
λ> action n k = replicateM k (shuffleM [1..n])
λ>
λ> :type action 10 6
action 10 6 :: (MonadRandom m, Num a, Enum a) => m [[a]]
λ>
λ> randomSeed = 42
λ> gen0 = mkStdGen randomSeed
λ>
λ> (xss,gen1) = runRand (action 10 6) gen0
λ>
λ> printAsLines zs = mapM_ (putStrLn . show) zs
λ>
λ> printAsLines xss
[10,7,4,6,3,9,1,8,2,5]
[4,1,3,2,7,8,10,9,6,5]
[2,8,6,9,1,5,7,4,3,10]
[7,10,5,2,9,1,6,4,8,3]
[3,7,4,10,8,1,2,5,6,9]
[9,1,2,4,3,8,7,6,5,10]
λ>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/427310.html
標籤:haskell
上一篇:haskell在哪里保存狀態?
下一篇:記錄中的Haskell可變向量
