Solved

Random User Input

Posted on 2013-01-31
4
370 Views
Last Modified: 2013-01-31
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.
0
Comment
Question by:saoj1981
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 38841517
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
http://msdn.microsoft.com/en-us/library/f02979c7.aspx
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

Michael
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 38841607
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.
0
 

Author Closing Comment

by:saoj1981
ID: 38841702
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?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38841808
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question