17 janvier 2011

Get a random element from an array

I used that expression before:
random_choice = array[rand(array.size)]

Then I read this quicktip and decided to try:
random_choice = array.choice

Unfortunately, on my production server, I still have Ruby 1.8.6. So, after a long search, I finally settled on:
random_choice = array.rand

Which is supported in both Ruby 1.8.6. and 1.8.7

Aucun commentaire:

Enregistrer un commentaire