-
Website
http://20bits.com -
Original page
http://20bits.com/articles/interview-questions-shuffling-an-array/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
prissypot13
3 comments · 1 points
-
Felix Purnama
4 comments · 1 points
-
hadley
2 comments · 1 points
-
adamheroku
2 comments · 3 points
-
twiss
2 comments · 1 points
-
-
Popular Threads
>> My next article is going to be more about the interview process rather than specific questions
Regarding the interview process, I've found this post very helpful.
http://www.joelonsoftware.com/articles/Guerrill...
Joel Spolsky has also written a book ("Smart and Gets Things Done") which elaborates on the same ideas in the blog post.
Nice post!
and shouldn't it be n= self.size in example#1 as well as example#2?
You're right about the first point, but not the second. Both self.size and size work because we're inside the Array instance.
However, since pragmatism trumps all, the "right" way to shuffle an Array in Ruby is this:
class Array
def shuffle!
replace(array.sort_by { rand })
end
end
As a bonus, it is a language/core lib bug and not your fault if there is a problem with the distribution ;)
That's not really true, though. In fact, that "solution" violates multiple requirements of the problems.
One, it's not in-place. Two, whether or not the distribution is uniform depends on the sorting algorithm that <tt>sort_by</tt> uses.
Great effort after reading this I really good very nice point how to shuffle array keep it up
and post more article also check Pre Developer
Thanks