nobu.nokada / softhome.net schreef: > Hi, > > At Thu, 16 Dec 2004 07:12:15 +0900, > Maarten Boonen wrote in [ruby-talk:123730]: > >>I made this a function a few days ago and it seems to be a lot faster. >>Not sure if it's completely correct though. >> >>def shuffle! >> a = 0 >> while(a<length) >> b = (length*Kernel.rand).to_i >> tmp = self[a] >> self[a] = self[b] >> self[b] = tmp >> a+=1 >> end >> self >>end > > > It wouldn't be uniform. An element which was close to the head > will tend to be placed close to the tail. > I tested this, by shuffeling an array [1..10] 100000 times and the elements at the head seem to be smaller (closer to the head again)? >ruby test2.rb 512468 528318 540408 550876 559504 566140 567602 565901 559436 549347 >Exit code: 0 >ruby test2.rb 512626 527151 539146 551026 560751 565943 565926 566866 560561 550004 >Exit code: 0 >ruby test2.rb 511651 526778 541792 551417 559825 563856 567336 566272 561004 550069 >Exit code: 0 class Array def shuffle! a = 0 while(a<length) b = (length*Kernel.rand).to_i tmp = self[a] self[a] = self[b] self[b] = tmp a+=1 end self end end total = Array.new(10,0) for i in 1..100000 arr = (1..10).to_a arr.shuffle! total.each_index do |index| total[index] += arr[index] end end puts total