Is the performance better if you skip swaps when i == j ? Also, for a swap method to give random results doesn't one need to swap from a random position in the array which has not been passed through yet? (see http://en.wikipedia.org/wiki/Shuffle noting Fisher- Yates shuffling.) -a On 23.4.2006, at 09:45, Himadri Choudhury wrote: > print ARGF.read.gsub!(/\B[a-z]+\B/) {|x| > x.length.times {|i| > j = rand(i+1) > x[j], x[i] = x[i] , x[j] > } > x > } > > Basically, this is an implementation of scrambling that uses swaps. I > remember this method for scrambling from way back, but I can't seem > to find > a good reference for it at the moment. > I also figured that this method would be faster since it is linear, > while > the sorts are n log(n) (n = length of the word) > > To by surprise, I found this method to actually be slower for any > normal > text. One possible explanation is that when words are relatively > short you > don't gain much from the n vs. nlogn difference, and you lose > because while > this method always has n swaps, sorting may have less.