in reply to Array Shuffle

Are you saying that *none* of the elements can be in the same position as the original array? If so, then I suppose you'd have to compare the two arrays after the shuffle, and reject the shuffle if you found any two elements equal.

BTW, is this by any chance a homework problem?

Replies are listed 'Best First'.
Re^2: Array Shuffle
by dunno260 (Novice) on Feb 28, 2006 at 05:57 UTC
    It is, and I am/was having a lot of trouble determining a solution to the given problem. Part of the problem is that I am relatively new to perl, and its been a while since I last programmed in Java, so I am quite rusty. However, I would not have just copied and pasted any code and put it off as my own either. More looking for some pseudo code/algorithm to accomplish this.

      OK, then how about this:

      1. Take your array, and make a copy.
      2. Shuffle one of them.
      3. Compare the elements of the arrays.
      4. If you find a match, then go back to step 2.
      5. If not, then quit - you've found a unique shuffling.

      I have to admit that this is a fairly simple-minded approach, but consider the source ;-). I don't doubt that there is a Perlish way of doing it - there always is. But I don't know it, yet.

      Here's another mechanism for you:

      1. Make a copy of your array
      2. shuffle it (using List::Util::shuffle)
      3. do an element-wise comparison of your two arrays and if you find a match swap that element with an adjacent element
      4. goto 3 until you swap no more