Swapping to scramble a list

Dec 08, 2007 14:45

I just recently found out something that should've been head-slappingly obvious. It's kind of embarrassing. Here is some code that I thought scrambled an array with every possible scrambling equally likely:

For every item in your array,
swap this item with a random element in the array
For each element in your array, there are N ways that the ( Read more... )

Leave a comment

Comments 1

nerdcodegeode December 9 2007, 07:14:41 UTC
Yeah..Something felt wrong about that algorithm. Still, I request that you not slap your head about it. Computer problems are so, so often hard as peaches..From the inside out.

Which is to say they're ridiculously hard at first, but, then, looking back, stupid easy.

Reply


Leave a comment

Up