Solved

Random User Input

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now