mirror of
https://github.com/redelmann/scat
synced 2025-01-09 22:24:19 +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:
parent
c9e460ad42
commit
e082a68c0f
@ -125,7 +125,7 @@ useup b = Builder $ \ n ->
|
||||
|
||||
-- | Shuffles the input list.
|
||||
shuffle :: [a] -> Builder [a]
|
||||
shuffle xs = fmap (perm xs) $ lessThan $ fact $ length xs
|
||||
shuffle xs = fmap (perm xs) $ lessThan $ fact $ fromIntegral $ length xs
|
||||
where
|
||||
fact :: Int -> Int
|
||||
fact :: Integer -> Integer
|
||||
fact n = product [1 .. n]
|
||||
|
@ -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
|
||||
-}
|
||||
|
||||
{- | Permutations, taken from the
|
||||
{- | Permutations, adapted from the
|
||||
http://en.literateprograms.org/Kth_permutation_(Haskell) webpage. -}
|
||||
module Scat.Utils.Permutation (perm) where
|
||||
|
||||
rr :: Int -> Int -> [Int]
|
||||
rr :: Integer -> Integer -> [Integer]
|
||||
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 = foldr (\ x rs -> x : [r + (if x <= r then 1 else 0) | r <- rs]) []
|
||||
dfr :: [Integer] -> [Integer]
|
||||
dfr = foldr (\ x rs -> x : [r + if x <= r then 1 else 0 | r <- rs]) []
|
||||
|
||||
-- | List permutation.
|
||||
perm :: [a] -> Int -> [a]
|
||||
perm xs k = [xs !! i | i <- dfr (rr (length xs) k)]
|
||||
perm :: [a] -> Integer -> [a]
|
||||
perm xs k = [xs !! fromInteger i | i <- dfr (rr (fromIntegral $ length xs) k)]
|
||||
|
Loading…
Reference in New Issue
Block a user