From: Ari Brown Date: 2007-04-24T11:24:57+09:00 Subject: Re: Shuffle Method I'm not gonna lie. You're a genius to me. Not only did you spot my problem and help me fix it, but you provided not 1, not 3, not 4, but 2 solutions to help me. You rock. Meh! Too many commands I haven't learned yet. But thank you SOOOOOO MUCHHHH On Apr 23, 2007, at 1:17 AM, Daniel Martin wrote: > def recurssive_shuffle some_array > n = some_array.length > new_array = [] > while n != 0 > r = rand(n) > helper_array = [] > while some_array.length != (r + 1) > helper_array.push some_array.pop > end > new_array.push some_array.pop > n = (n - 1) > helper_array.each do |item| > some_array.push item > end > end > puts new_array > end > > However, it occurs to me that this algorithm is easier to express as: > > # "recursive" only has one "s" > def recursive_shuffle some_array > new_array = [] > while ! some_array.empty? > r = rand(some_array.length) > new_array.push some_array.delete_at(r) > helper_array = some_array.slice!(r..-1) > helper_array.reverse! > some_array.concat helper_array > end > puts new_array > end > > It took me a while to realize that you were reversing the bits you'd > pulled off. > > I'll also note that the reversing doesn't affect the randomness of > your shuffle at all. The last three lines of that while loop > (everything dealing with helper_array) could be dropped without > affecting anything. The sort_by { rand } method is almost certainly > faster, or you could go and implement a standard Fisher-Yates in-place > shuffle: > > # shuffles some_array in-place > def shuffle some_array > 2.upto(some_array.length) do |n| > r = rand(n) > some_array[n-1],some_array[r] = some_array[r],some_array[n-1] > end > some_array > end > > (or open up Array, and add this method as shuffle!) -------------------------------------------------------| ~ Ari crap my sig won't fit