From: Rob Biedenharn Date: 2008-07-04T01:14:15+09:00 Subject: Re: pseudo-randomize an array in a consistent order On Jul 3, 2008, at 11:57 AM, Max Williams wrote: > Thanks guys > > This is what i did in the meantime since asking (I monkey-patched > Array): > > class Array > > def randomize(seed=nil) > srand(seed) if seed > self.sort{|a,b| rand <=> rand } > end > > def randomize!(seed=nil) > srand(seed) if seed > self.sort!{|a,b| rand <=> rand } > end > > end > > I'm worried though that using sort like this is a bit inefficient. > The > array being sorted is around 3000 numbers (and not likely to exceed > 5000) so maybe that's not too much of an issue. Is there a more > efficient way you can think of? Use sort_by{rand} and you'll call rand once per element (O(n)) rather than twice per comparison (O(n*log(n))) -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com