Only began using Haskell a couple of weeks ago – I am attempting to randomly shuffle a list of type Card by splitting the list into two at a random point int eh list (depending on an array of random integers produced by the randomList function) and swapping the order of these two parts a number of times, but the output is not at all random, and the parse only seems to be happening once, pretty desperate as I need it working and the deadline is tonight!
randomList :: (Random a) => (a,a) -> Int -> StdGen -> [a]
randomList bnds n = take n . randomRs bnds
randomise :: [Int] -> [Card] -> [Card]
randomise [] p = p
randomise (x : xs) p = do
randomise xs ((drop x p) ++ (take x p))
shuffle :: Int -> [Card] -> [Card]
shuffle r p = do
let g = mkStdGen r
randomise(randomList (1, (length p)-1) 500 g :: [Int]) p
You can just make a random number of permutations on your list. You can do it like:
permutationmakesnpermutations on the (assumed sorted) listxs. When randomizing,xsneed not be sorted however.facis just an implementation of the factorial function.shufflemakes a random number and applies that many permutations toxs.It’s a bit different from what you are trying to do, but it works wonders. I assumed you didn’t need to explicitly use your proposed method. You will have to implement
permutationandfacyourself though.For help on
permutation, you could look here. It’s a description to solve a Project Euler Problem, but you could use the same procedure to makenpermutations.EDIT: I don’t know if anyone cares anymore, but I found another way to do it WAY easier: