Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomizing array elements

Tags:

ruby

shuffle

I have an array @number = [1,2,3,4,5,6,7,8,9]
Now, I want to randomize the array content... something like eg: [5,3,2,6,7,1,8]
Please guide me how to proceed with it.

like image 248
nirmal Avatar asked Sep 29 '10 04:09

nirmal


2 Answers

Use the shuffle method ...

irb(main):001:0> [1,2,3,4,5].shuffle
=> [3, 4, 2, 5, 1]
like image 96
Dave Pirotte Avatar answered Oct 03 '22 12:10

Dave Pirotte


the shuffle command returns a randomized version of an array

eg:

[1,2,3].shuffle => [2,3,1]
like image 38
Nikolaus Gradwohl Avatar answered Oct 03 '22 11:10

Nikolaus Gradwohl