From: Max Williams on
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-forum.com/.

From: Pascal J. Bourguignon on
Max Williams <toastkid.williams(a)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__
From: David A. Black on
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.rubypal.com for details and updates!

From: Max Williams on
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-forum.com/.

From: Robert Dober on
a.sort{ rand }

but I do not know about a seed.

Cheers
Robert

--
http://ruby-smalltalk.blogspot.com/

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