Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > sort_by { rand } not working

Reply
Thread Tools

sort_by { rand } not working

 
 
Mike Dershowitz
Guest
Posts: n/a
 
      08-26-2007
Hello:

I've got an array of arrays that I'd like to sort_by random. each
individual array is just a hash of a value and then an object. Is there
some reason why sort_by { rand } wouldn't work? My code is simple (teams
is populated with a db call):

@teams.each do |t|
@all << ["t",t]
end
#current not working
@all.sort_by { rand }

Any ideas? Am I stretching the limit of the rand function?

Thanks!

Mike
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      08-26-2007
On Aug 26, 7:25 am, Mike Dershowitz <michael.dershow...@jpmchase.com>
wrote:
> @teams.each do |t|
> @all << ["t",t]
> end
> #current not working
> @all.sort_by { rand }


#sort_by doesn't modify the original array, it returns a new one.

Try @all = @all.sort_by{ rand }


 
Reply With Quote
 
 
 
 
Ken Bloom
Guest
Posts: n/a
 
      08-26-2007
On Sun, 26 Aug 2007 22:25:08 +0900, Mike Dershowitz wrote:

> Hello:
>
> I've got an array of arrays that I'd like to sort_by random. each
> individual array is just a hash of a value and then an object. Is there
> some reason why sort_by { rand } wouldn't work? My code is simple (teams
> is populated with a db call):
>
> @teams.each do |t|
> @all << ["t",t]
> end
> #current not working
> @all.sort_by { rand }


sort_by returns the sorted list
sort_by! sorts the list in place

--Ken

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
 
Reply With Quote
 
Ken Bloom
Guest
Posts: n/a
 
      08-26-2007
On Sun, 26 Aug 2007 09:22:38 -0500, Ken Bloom wrote:

> On Sun, 26 Aug 2007 22:25:08 +0900, Mike Dershowitz wrote:
>
>> Hello:
>>
>> I've got an array of arrays that I'd like to sort_by random. each
>> individual array is just a hash of a value and then an object. Is
>> there some reason why sort_by { rand } wouldn't work? My code is simple
>> (teams is populated with a db call):
>>
>> @teams.each do |t|
>> @all << ["t",t]
>> end
>> #current not working
>> @all.sort_by { rand }

>
> sort_by returns the sorted list
> sort_by! sorts the list in place


Whoops. As soon as I posted, I noticed that there is no sort_by!.

--Ken

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      08-26-2007
On 8/26/07, Ken Bloom <> wrote:
> Whoops. As soon as I posted, I noticed that there is no sort_by!.


try

sort!{rand}


kind regards -botp

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      08-26-2007
On Aug 26, 2007, at 12:31 PM, botp wrote:

> On 8/26/07, Ken Bloom <> wrote:
>> Whoops. As soon as I posted, I noticed that there is no sort_by!.

>
> try
>
> sort!{rand}


That's not a random sort. In fact, it's equivalent to sort! { 1 }:

>> data =3D (0..9).to_a

=3D> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> data.sort! { rand }

=3D> [9, 5, 0, 6, 2, 7, 4, 8, 3, 1]
>> data.sort! { rand }

=3D> [1, 7, 9, 4, 0, 8, 2, 3, 6, 5]
>> data.sort! { rand }

=3D> [5, 8, 1, 2, 9, 3, 0, 6, 4, 7]
>> data =3D (0..9).to_a

=3D> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> data.sort! { 1 }

=3D> [9, 5, 0, 6, 2, 7, 4, 8, 3, 1]
>> data.sort! { 1 }

=3D> [1, 7, 9, 4, 0, 8, 2, 3, 6, 5]
>> data.sort! { 1 }

=3D> [5, 8, 1, 2, 9, 3, 0, 6, 4, 7]

It would be better to use:

data =3D data.sort_by { =85 }

James Edward Gray II=

 
Reply With Quote
 
Stefan Rusterholz
Guest
Posts: n/a
 
      08-26-2007
botp wrote:
> On 8/26/07, Ken Bloom <> wrote:
>> Whoops. As soon as I posted, I noticed that there is no sort_by!.

>
> try
>
> sort!{rand}
>
>
> kind regards -botp


That's equivalent to sort! { 1 } and rather predictable (sort/sort!
expects -1, 0 or 1 as value, rand is 0..1) and hence IMHO pointless.

Regards
Stefan
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Stefan Rusterholz
Guest
Posts: n/a
 
      08-26-2007
James Gray wrote:
> On Aug 26, 2007, at 12:31 PM, botp wrote:
>
>> On 8/26/07, Ken Bloom <> wrote:
>>> Whoops. As soon as I posted, I noticed that there is no sort_by!.

>>
>> try
>>
>> sort!{rand}

>
> That's not a random sort. In fact, it's equivalent to sort! { 1 }:
>
> >> data = (0..9).to_a

> => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> >> data.sort! { rand }

> => [9, 5, 0, 6, 2, 7, 4, 8, 3, 1]
> >> data.sort! { rand }

> => [1, 7, 9, 4, 0, 8, 2, 3, 6, 5]
> >> data.sort! { rand }

> => [5, 8, 1, 2, 9, 3, 0, 6, 4, 7]
> >> data = (0..9).to_a

> => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> >> data.sort! { 1 }

> => [9, 5, 0, 6, 2, 7, 4, 8, 3, 1]
> >> data.sort! { 1 }

> => [1, 7, 9, 4, 0, 8, 2, 3, 6, 5]
> >> data.sort! { 1 }

> => [5, 8, 1, 2, 9, 3, 0, 6, 4, 7]
>
> It would be better to use:
>
> data = data.sort_by { � }
>
> James Edward Gray II


Well, it has the (extremely little) chance of being 0, which introduces
a very very small randomness

Regards
Stefan
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Lionel Bouton
Guest
Posts: n/a
 
      08-26-2007
James Edward Gray II wrote:
>
> It would be better to use:
>
> data = data.sort_by { … }


I was wondering about a crazy idea of mine: "sort!{rand<=>rand}" and
from my bench results it's a waste of time (it's even dangerous, see
below). I suspect sort_by begins by mapping the block results and then
sorts based on the map (it's roughly 4x faster than sort!{ rand <=> rand }).
sort!{rand<=>rand} obviously can't do that and must call the block each
time a comparison must be done (I suspect that more rand calls make it
slower even if it can avoid copying things and instead do in-place
modifications ... or that the Ruby sort algorithm doesn't like
comparison results changing...).

Obviously depending on the sort algorithm used it might not even
converge on a result (the dangerous part...).

Lionel

 
Reply With Quote
 
Dan Zwell
Guest
Posts: n/a
 
      08-26-2007
Mike Dershowitz wrote:
> Hello:
>
> I've got an array of arrays that I'd like to sort_by random. each
> individual array is just a hash of a value and then an object. Is there
> some reason why sort_by { rand } wouldn't work? My code is simple (teams
> is populated with a db call):
>
> @teams.each do |t|
> @all << ["t",t]
> end
> #current not working
> @all.sort_by { rand }
>
> Any ideas? Am I stretching the limit of the rand function?
>
> Thanks!
>
> Mike


Mike,

This has been discussed here before. Your two best choices are:
@all = @all.sort_by {rand}
or

for i in 0...@all.length
j = i+rand(@all.length-i)
@all[i], @all[j] = @all[j], @all[i]
end

The second one will be faster, but you probably shouldn't care on small
arrays.

Dan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sort_by is not stable ? Michel Demazure Ruby 20 12-05-2010 10:18 AM
rand() v. rand(0.1) ? 7stud -- Ruby 6 09-16-2007 03:18 PM
sort_by{rand} doesn't shuffle array Pat Maddox Ruby 4 01-03-2006 12:12 PM
stable sort_by? Patrick Gundlach Ruby 11 12-14-2005 06:03 PM
A sort_by descending sort Michael Gaunnac Ruby 8 10-09-2004 03:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57