From: eden li Date: 2007-04-23T12:15:43+09:00 Subject: Re: Shuffle Method Why not just do a random sort instead? >> some_array = ['cat', 'dog', 'fun', 'zoo'] => ['cat', 'dog', 'fun', 'zoo'] >> some_array.sort { rand(3) - 1 } => ["zoo", "dog", "fun", "cat"] On Apr 23, 8:59 am, Ari Brown wrote: > Once again, hello > > Normally I'd go to my dad for something like this. But since he's out > and you all are hyperactive posters... > > I've been writing my own shuffling method, and I end up getting > doubles in it. > For instance, output might say something like: > fun > cat > zoo > zoo > > Now, it's NOT supposed to do that, but it does anyways. I am clueless > as to what the problem is. What I DID notice, however (When using > source 2) was that it seemed to pop off the last item in the array. > Repeatedly. Yes, it's supposed to do that, but the last item changes > each time. How does it keeping popping off, say, 'fun' repeatedly? > > Lotsa source code here, but I appreciate the help. Thanks > ridonculously a lot. > > --------------------------------------------------------------| > SOURCE 1 > --------------------------------------------------------------| > some_array= ['cat', 'dog', 'fun', 'zoo'] > > def shuffle some_array > recurssive_shuffle some_array > end > > def recurssive_shuffle some_array > n = some_array.length > helper_array = [] > new_array = [] > while n != 0 > r = rand(n) > 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 > > shuffle some_array > --------------------------------------------------------------| > SOURCE 2 > --------------------------------------------------------------| > some_array = ['cat', 'dog', 'fun', 'zoo'] > > def shuffle some_array > recurssive_shuffle some_array > end > > def recurssive_shuffle some_array > n = some_array.length > helper_array = [] > new_array = [] > while n != 0 > r = rand(n) > while some_array.length != (r + 1) > helper_array.push some_array.pop > puts 'Array' > puts helper_array > puts > 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 > > shuffle some_array > > ---------------------------------------------------------------| > ~Ari > "I don't suffer from insanity. I enjoy every minute of it" --1337est > man alive