[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

pseudo-randomize an array in a consistent order

Max Williams

7/3/2008 2:56:00 PM

Does anyone know how to pseudo-randomize an array (eg with a seed) so
that you get the same order every time you do it?

thanks
max
--
Posted via http://www.ruby-....

29 Answers

pjb

7/3/2008 3:30:00 PM

0

Max Williams <toastkid.williams@gmail.com> writes:

> Does anyone know how to pseudo-randomize an array (eg with a seed) so
> that you get the same order every time you do it?

Using a pseudo-random number generator, seeded with the same seed.

Another way would be to just use the seed to index the permutations of
the array, but you would need big seeds for non-small arrays...


--
__Pascal Bourguignon__

David A. Black

7/3/2008 3:35:00 PM

0

Hi --

On Thu, 3 Jul 2008, Max Williams wrote:

> Does anyone know how to pseudo-randomize an array (eg with a seed) so
> that you get the same order every time you do it?

Use Kernel#srand.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

Max Williams

7/3/2008 3:58:00 PM

0

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?

--
Posted via http://www.ruby-....

Robert Dober

7/3/2008 4:04:00 PM

0

a.sort{ rand }

but I do not know about a seed.

Cheers
Robert

--
http://ruby-smalltalk.blo...

---
AALST (n.) One who changes his name to be further to the front
D.Adams; The Meaning of LIFF

James Gray

7/3/2008 4:08:00 PM

0

On Jul 3, 2008, at 11:04 AM, Robert Dober wrote:

> a.sort{ rand }

Don't do that:

>> a = (1..5).to_a
=> [1, 2, 3, 4, 5]
>> a.sort { rand }
=> [5, 3, 1, 4, 2]
>> a.sort { rand }
=> [5, 3, 1, 4, 2]
>> a.sort { rand }
=> [5, 3, 1, 4, 2]

Use a.sort_by { rand } instead.

James Edward Gray II

David A. Black

7/3/2008 4:09:00 PM

0

Hi --

On Fri, 4 Jul 2008, 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?

I absolutely wouldn't worry about it, unless you actually notice any
slowness.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

James Gray

7/3/2008 4:10:00 PM

0

On Jul 3, 2008, at 11:08 AM, James Gray wrote:

> On Jul 3, 2008, at 11:04 AM, Robert Dober wrote:
>
>> a.sort{ rand }
>
> Don't do that:
>
> >> a = (1..5).to_a
> => [1, 2, 3, 4, 5]
> >> a.sort { rand }
> => [5, 3, 1, 4, 2]
> >> a.sort { rand }
> => [5, 3, 1, 4, 2]
> >> a.sort { rand }
> => [5, 3, 1, 4, 2]

Oh, you meant to show this behavior. My bad.

James Edward Gray II

Rob Biedenharn

7/3/2008 4:14:00 PM

0

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://agileconsult...
Rob@AgileConsultingLLC.com



Max Williams

7/3/2008 4:16:00 PM

0

David A. Black wrote:

> I absolutely wouldn't worry about it, unless you actually notice any
> slowness.

Cool, if there's a problem i'll say David A. Black told me it would be
ok :)

thanks everyone


--
Posted via http://www.ruby-....

David A. Black

7/3/2008 4:16:00 PM

0

Hi --

On Fri, 4 Jul 2008, James Gray wrote:

> On Jul 3, 2008, at 11:08 AM, James Gray wrote:
>
>> On Jul 3, 2008, at 11:04 AM, Robert Dober wrote:
>>
>>> a.sort{ rand }
>>
>> Don't do that:
>>
>> >> a = (1..5).to_a
>> => [1, 2, 3, 4, 5]
>> >> a.sort { rand }
>> => [5, 3, 1, 4, 2]
>> >> a.sort { rand }
>> => [5, 3, 1, 4, 2]
>> >> a.sort { rand }
>> => [5, 3, 1, 4, 2]
>
> Oh, you meant to show this behavior. My bad.

It's not really even pseudo-random, though -- it's just what you get
if you always say that a <=> b is > 0. [1,2,3,4,5].sort { 1 } does the
same thing. So every 5-element array will shuffle the same way, which
I don't think is what the OP wanted.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!