Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 1st, 2011, 03:35 PM
ranmasdarkheart ranmasdarkheart is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 2 ranmasdarkheart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 1st, 2011, 05:36 PM
TheOtherDino's Avatar
TheOtherDino TheOtherDino is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Location: Katy, Texas
Posts: 489 TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 4 Days 18 h 25 m 29 sec
Reputation Power: 198
You can use the rand() kernel method to pick a number using rand(array.size).

kernel.rand

Reply With Quote
  #3  
Old January 1st, 2011, 05:38 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 57 m 20 sec
Reputation Power: 1736
take a look at this:
http://rubylearning.com/satishtalim/ruby_random_numbers.html

Reply With Quote
  #4  
Old January 1st, 2011, 09:16 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
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

Reply With Quote
  #5  
Old January 4th, 2011, 09:25 PM
ranmasdarkheart ranmasdarkheart is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 2 ranmasdarkheart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Need help with arrays

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap