1
0
mirror of https://github.com/redelmann/scat synced 2025-01-10 06:34:20 +01:00

Switched to Integer instead of Int. Using Int's leaded to incorrectly generated passwords when shuffling was involved, especially on 32-bits.

This commit is contained in:
Romain Edelmann 2014-04-25 22:54:19 +02:00
parent c9e460ad42
commit e082a68c0f
2 changed files with 11 additions and 9 deletions

View File

@ -125,7 +125,7 @@ useup b = Builder $ \ n ->
-- | Shuffles the input list. -- | Shuffles the input list.
shuffle :: [a] -> Builder [a] shuffle :: [a] -> Builder [a]
shuffle xs = fmap (perm xs) $ lessThan $ fact $ length xs shuffle xs = fmap (perm xs) $ lessThan $ fact $ fromIntegral $ length xs
where where
fact :: Int -> Int fact :: Integer -> Integer
fact n = product [1 .. n] fact n = product [1 .. n]

View File

@ -25,17 +25,19 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Retrieved from: http://en.literateprograms.org/Kth_permutation_(Haskell)?oldid=16316 Retrieved from: http://en.literateprograms.org/Kth_permutation_(Haskell)?oldid=16316
-} -}
{- | Permutations, taken from the {- | Permutations, adapted from the
http://en.literateprograms.org/Kth_permutation_(Haskell) webpage. -} http://en.literateprograms.org/Kth_permutation_(Haskell) webpage. -}
module Scat.Utils.Permutation (perm) where module Scat.Utils.Permutation (perm) where
rr :: Int -> Int -> [Int] rr :: Integer -> Integer -> [Integer]
rr 0 _ = [] rr 0 _ = []
rr n k = k `mod` n : rr (n - 1) (k `div` n) rr n k = m : rr (n - 1) d
where
(d, m) = k `divMod` n
dfr :: [Int] -> [Int] dfr :: [Integer] -> [Integer]
dfr = foldr (\ x rs -> x : [r + (if x <= r then 1 else 0) | r <- rs]) [] dfr = foldr (\ x rs -> x : [r + if x <= r then 1 else 0 | r <- rs]) []
-- | List permutation. -- | List permutation.
perm :: [a] -> Int -> [a] perm :: [a] -> Integer -> [a]
perm xs k = [xs !! i | i <- dfr (rr (length xs) k)] perm xs k = [xs !! fromInteger i | i <- dfr (rr (fromIntegral $ length xs) k)]