Random User Input

I need help writing a C# code that ask the user to input 20 numbers between 50 and 100 that does not duplicate any number.  This would be a looping proram.
Who is Participating?
Michael FowlerConnect With a Mentor Solutions ConsultantCommented:
Sorry I have to ask but is this a homework assignment?

To solve this you would need a couple of checks against user input

1/ Has the user entered a number. Have a look at Int32.TryParse
2/ Is the number within the acceptable range
3/ Has the number been entered already Have a llok at List<T>.Contains http://msdn.microsoft.com/en-AU/library/bhkz42b3.aspx

Hope this helps

I'm sure this is an assignment :).

And you know the rules: we can't write it for you. But definitely we may give hints.

1. Create:
 - a dictionary (a hash table) that contains result numbers. The dictionary is good in the sense of performance, it is very fast and easy to check if it contains a specific key (randomly generated number)

2. Organize a while loop. This loop will check if dictionary contains less than 20 items.

3. Inside the loop - generate a random number. Random numbers in a region are generated like:

Random rnd = new Random();
int n = rnd.Next(50, 100); // creates a number between 50 and 100

Open in new window

4. Check if this number presents in the dictionary. If not - add to the dictionary, if yes - generate another number.

really, steps 3 and 4 are to be inside another loop that run until not existing random number is generated.

5. When the dictionary has 20 items - quit. If required, convert the dictionary keys to a list or whatever you need.
saoj1981Author Commented:
I've tried the random method. My problem is how do I get the program to read what the user has put in as the random?
Mike TomlinsonMiddle School Assistant TeacherCommented:
Is this a Console application?  If yes, use Console.ReadLine() and convert the returned String to an Integer using the previously mentioned Int32.TryParse().

For a WinForms application, place a TextBox on your form.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.