|
From: Max Williams on 3 Jul 2008 10:56 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 3 Jul 2008 11:30 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 3 Jul 2008 11:35 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 3 Jul 2008 11:57 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 3 Jul 2008 12:04 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
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Software for web-based private discussion groups Next: Check version |