School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,096 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,030 people online right now. Registration is fast and FREE... Join Now!




Using random number generators.

 

Using random number generators.

rogersj7

30 Oct, 2008 - 04:18 PM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2008
Posts: 3

Been trying to figure out code that incorporates a random number generator. I know the basics of the code:

CODE
rand(1000)+1[/code}

This will give a random number anywhere from 1 to 1000.

What I'm having issue with is prompting the user to enter a group of horses names. Then I want to plug the names into the generator and come out with first, second, third, etc place.

[code]
print "Enter the number of horses: "
         number=gets.chomp.to_i

horse=[]
num1=1

for i in 0..number-1

print "Enter the name of horse # "num1.to_s + ": "
racer=gets.chomp.to_s

horse.push(racer)


Where and how to I use the random number generator?

Thanks in advance,

J.




User is offlineProfile CardPM
+Quote Post


MitkOK

RE: Using Random Number Generators.

30 Oct, 2008 - 11:27 PM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Aug, 2007
Posts: 356



Thanked: 19 times
Dream Kudos: 250
My Contributions
You want to do what ?
User is offlineProfile CardPM
+Quote Post

Kingbradley6

RE: Using Random Number Generators.

3 Nov, 2008 - 09:10 AM
Post #3

D.I.C Head
Group Icon

Joined: 10 Jul, 2008
Posts: 134



Thanked: 2 times
Dream Kudos: 100
My Contributions
I think you need to add the random function in later on rather than at the beginning.

Do your code as required then when you want to pick the winner use, for example....

Use randomNumber = rand(numberOfHorses)

Then use that random number to pick out one of your indexes in the array holding all the horses.

Print the horse and it's place (depending on how many you have in the first place), then delete the object from your array so it can't have more than one place (e.g. so it can't come first and second).

Repeat that step but obviously with the 'numberOfHorses' being 1 less and the position of that horse (e.g. first, second, third, etc) 1 more.

I THINK this will work.

If I get time I will try to add the code but right now I'm doing some history coursework and don't have the time!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 11:51AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month