Randomizing an Array?

J

jwcooper

I'm trying to randomize an array, and this is what I have:

arr_set_unordered = randomizer(arr_set)

def randomizer(arr)
result = arr.collect { arr.slice!(rand arr.length) }
end

It does randomize it, but it only returns 3 values instead of the 5
values that I'm expecting. Any ideas why?

Thank you!
 
D

Daniel Waite

jwcooper said:
I'm trying to randomize an array...

Try this.

class Array

def randomize
duplicated_original, new_array = self.dup, self.class.new
new_array <<
duplicated_original.slice!(rand(duplicated_original.size)) until
new_array.size.eql?(self.size)
new_array
end

def randomize!
self.replace(randomize)
end

end

Enjoy!
 
D

Daniel Waite

Sorry, I guess a use-case would be helpful.

Call #randomize or #randomize! on any array to mix up its elements.

[ 5, 10, 15, 20, 25 ].randomize # [25, 15, 5, 20, 10]
 
D

Dan Stevens (IAmAI)

I found a library called 'rand.rb':
http://raa.ruby-lang.org/project/rand/. It allows you to select an
element at random from an array and 'scramble' (randomize the order)
of them as well.

Description:

Helper methods for Enumerable, Array, Hash, and String
that let you pick a random item or shuffle the order of items.

Examples with Array:

[1,2,3,4].pick
# => 2 (pick random element)

[1,2,3,4].shuffle
# => [2, 4, 1, 3] (return random-sorted version)

a = [1,2,3,4]
a.pick!
# => 3
a
# => [1,2,4] (remove the picked element)
 
I

Ilan Berci

jwcooper said:
I'm trying to randomize an array, and this is what I have:

arr_set_unordered = randomizer(arr_set)

def randomizer(arr)
result = arr.collect { arr.slice!(rand arr.length) }
end

It does randomize it, but it only returns 3 values instead of the 5
values that I'm expecting. Any ideas why?

Thank you!

ilans-Mac:~ ilan$ irb
irb(main):001:0> (1..10).to_a.sort {rand}
=> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]
irb(main):002:0>
 
F

Florian Frank

Ilan said:
jwcooper said:
I'm trying to randomize an array, and this is what I have:
[...]
ilans-Mac:~ ilan$ irb
irb(main):001:0> (1..10).to_a.sort {rand}
=> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]
irb(main):002:0>
Don't do this, better use (1..10).sort_by { rand }. Your version is
equivalent to (1..10).sort { 1 } and *always* creates the same
permutation for this array.
 
D

Daniel Waite

Florian said:
Don't do this, better use (1..10).sort_by { rand }. Your version is
equivalent to (1..10).sort { 1 } and *always* creates the same
permutation for this array.

It's random for me. Both work.
 
I

Ilan Berci

Daniel said:
It's random for me. Both work.

Actually Frank is correct, my mistake, it should definetely be sort_by
and not sort.. I should have reviewed it more thoroughly before
responding..

ilan
 
F

Florian Frank

Daniel said:
It's random for me. Both work.

No, really, it isn't. It may "look random", but try sorting (1..10) many
times and ponder the coincidence.
 
J

Joao Silva

Florian said:
No, really, it isn't. It may "look random", but try sorting (1..10) many
times and ponder the coincidence.

That's because sort{ foo } expects foo to be in -1,0,1.

rand() is always [0, 1).

rand(2)-1 is (-1,1) however and thus will work just fine. :)

One could do sort{rand <=> rand}, but that's ~9x slower by my testing.

irb(main):163:0> (1..10).to_a.sort{rand }
=> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]
irb(main):164:0> (1..10).to_a.sort{rand }
=> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]
irb(main):165:0> (1..11).to_a.sort{rand }
=> [11, 6, 1, 7, 3, 8, 5, 9, 4, 10, 2]
irb(main):166:0> (1..12).to_a.sort{rand }
=> [12, 7, 1, 8, 3, 9, 5, 10, 2, 11, 6, 4]

vs

irb(main):186:0> (1..12).to_a.sort{rand(2) -1}
=> [1, 2, 6, 11, 9, 4, 8, 7, 5, 10, 3, 12]
irb(main):187:0> (1..12).to_a.sort{rand(2) -1}
=> [1, 9, 8, 3, 5, 6, 11, 4, 2, 10, 7, 12]


- Sai http://saizai.com, who is too lazy to remember his actual account
pass right now
 
R

Robert Klemme

2008/5/15 Joao Silva said:
That's because sort{ foo } expects foo to be in -1,0,1.

I don't think so - the sign is important:

irb(main):001:0> (1..10).sort { -1 }
=> [1, 2, 3, 7, 4, 9, 5, 8, 6, 10]
irb(main):002:0> (1..10).sort { 1 }
=> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]
irb(main):003:0> (1..10).sort { 10 }
=> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]
irb(main):004:0> (1..10).sort { -10 }
=> [1, 2, 3, 7, 4, 9, 5, 8, 6, 10]
rand() is always [0, 1).

Did you mean [0,1[?
rand(2)-1 is (-1,1) however and thus will work just fine. :)

rand(2)-1 is (-1,0). I would rather do rand(3)-1 or rand(2) - 0.5 -
But all this is worse than sort_by { rand } IMHO.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top