Link to home
Start Free TrialLog in
Avatar of saoj1981
saoj1981

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dmitry G
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.
Avatar of saoj1981
saoj1981

ASKER

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?
Is this a Console application?  If yes, use Console.ReadLine() and convert the returned String to an Integer using the previously mentioned Int32.TryParse().
http://msdn.microsoft.com/en-us/library/system.console.readline.aspx

For a WinForms application, place a TextBox on your form.