Solved

Random User Input

Posted on 2013-01-31
4
368 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
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 29

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 85

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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