5 Replies - 5089 Views - Last Post: 10 January 2008 - 09:34 AM Rate Topic: -----

#1 shivangee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-January 08

how do use the random

Post icon  Posted 09 January 2008 - 11:46 AM

i have been asked to write a program that asks the user to enter 5 names which it stores in an array. Then get it to pick one of these names at random and declare that person is the winner. i got this so far
 string[] forename = new string[6];

			Console.WriteLine("PLease enter name 1:");
				string name1 = Console.ReadLine();
				forename[0] = name1;

				Console.WriteLine("PLease enter name 2:");
				string name2 = Console.ReadLine();
				forename[1] = name2;


				Console.WriteLine("PLease enter name 3:");
				string name3 = Console.ReadLine();
				forename[2] = name2;



				Console.WriteLine("PLease enter name 4:");
				string name4 = Console.ReadLine();
				forename[3] = name4;


				Console.WriteLine("PLease enter name 5:");
				string name5 = Console.ReadLine();
				forename[4] = name5;

				Console.ReadLine();

but i don't how to get it to choose a name at random. using the Random thing, i can do it with number but cant with words as i need to use a for statement which i only know how to use with numbers not words.
Please Please try to help it is an emergency
Thank You
Shivangee x

This post has been edited by jayman9: 10 January 2008 - 10:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: how do use the random

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: how do use the random

Posted 09 January 2008 - 11:51 AM

I believe this belongs to the C# forum.
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,341
  • Joined: 20-August 07

Re: how do use the random

Posted 09 January 2008 - 11:58 AM

This isn't C++, it looks like C#. You will likely get a better response on another forum :)
Was This Post Helpful? 0
  • +
  • -

#4 ash_b087  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-January 08

Re: how do use the random

Posted 10 January 2008 - 02:57 AM

hi shivangee

i dont really understand the code u ve written...
if u r using a array of strings, u store the names accordingly and then u generate a random number... the random number will be the position of the array... then u display the array...

if random number is 5, u display array

printf("%s",array[5]); /*thats in C*/

tc
ashna
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: how do use the random

Posted 10 January 2008 - 06:41 AM

int num = random.Next(5);


this will generate a random number less than 5. You can then print out the value of the element at that index in the array.

Moved to C#
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5883
  • View blog
  • Posts: 12,767
  • Joined: 16-October 07

Re: how do use the random

Posted 10 January 2008 - 09:34 AM

I'm guessing you want something like this.

int numberOfPeople = 6;
string[] forename = new string[numberOfPeople];
for (int i = 0; i < numberOfPeople; i++) {
	Console.WriteLine("Please enter name {0}:", (i + 1));
	forename[i] = Console.ReadLine();
}
Random rnd = new Random();
Console.WriteLine("Random Person is {0}.", forename[rnd.Next(numberOfPeople-1)]);



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1