Solved

Random User Input

Posted on 2013-01-31
4
363 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

16 Experts available now in Live!

Get 1:1 Help Now