### Thread: Need help with arrays

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2011
Posts
2
Rep Power
0

#### Need help with arrays

ok heres the deal i have a bunch of data i have aranged into arrrays i want to pick a single number from my array at random, i have been looking online for about 4 hours and the only thing i found similar was to randomly rearrange the numbers in the array
a friend showed me how to do it once but i have just plain forgotten how to do it
2. You can use the rand() kernel method to pick a number using rand(array.size).

kernel.rand
3. take a look at this:
http://rubylearning.com/satishtalim/ruby_random_numbers.html
4. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Jan 2004
Location
Constant Limbo
Posts
989
Rep Power
367
If you are looking for a random element of an array that is already filled ou can use sort_by.

Example:
Code:
```irb(main):001:0> a = (1..20).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
irb(main):002:0> a.sort_by { rand }
=> [12, 3, 1, 19, 14, 17, 16, 13, 8, 4, 9, 5, 15, 20, 11, 10, 2, 7, 6, 18]
irb(main):003:0> a.sort_by { rand }
=> [14, 9, 19, 3, 20, 13, 16, 12, 2, 11, 6, 17, 1, 8, 5, 15, 10, 18, 7, 4]
irb(main):004:0> a.sort_by { rand }
=> [20, 12, 5, 14, 11, 3, 17, 6, 1, 13, 4, 15, 2, 18, 10, 8, 19, 16, 7, 9]
irb(main):005:0>```
Just grab the first element of the shuffled array.
Depending on your version of ruby you may also have the Array#shuffle which has the same effect as the example above.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2011
Posts
2
Rep Power
0

#### thanks guys

heres how i went about it

puts([rand(56)+1])

i also was able to do it this way

draws[0] = [(ns1[rand(16)])]
draws[1] = [(ns2[rand(18)])]
draws[2] = [(ns3[rand(25)])]
draws[3] = [(ns4[rand(17)])]
puts(draws)

thanks for all your help, i figured this out about 15 minutes after i posted jsut by playing around with ruby a bit still new to the whole scene so i really do apprciate the help