The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Ruby Programming
|
Need help with arrays
Discuss Need help with arrays in the Ruby Programming forum on Dev Shed. Need help with arrays Ruby and Ruby on Rails programming forum covering Ruby Tips and Tricks, Best Practices, and agile development with Ruby on Rails.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 1st, 2011, 03:35 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 2
Time spent in forums: 44 m 33 sec
Reputation 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
|

January 1st, 2011, 05:36 PM
|
 |
Contributing User
|
|
Join Date: Jan 2010
Location: Katy, Texas
|
|
You can use the rand() kernel method to pick a number using rand(array.size).
kernel.rand
|

January 1st, 2011, 05:38 PM
|
 |
Lord of the Dance
|
|
|
|
|
take a look at this:
http://rubylearning.com/satishtalim/ruby_random_numbers.html
|

January 1st, 2011, 09:16 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Constant Limbo
|
|
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.
__________________
True happiness is not getting what you want, it's wanting what you've already got.
My Blog
|

January 4th, 2011, 09:25 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 2
Time spent in forums: 44 m 33 sec
Reputation 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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|