Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me...
I need to create an application that takes names from a string (or array, or whatever might be useful) and display it. plus when one name is displayed it should not shown again. (by the way there will be 35 names in the string).
Thanks in advance for all your answers...
Posted
Comments
OriginalGriff 5-May-15 1:48am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So work out what distinguishes one name from another, give us an example of your input string, and an example of the names you expect to get from it!

Use the "Improve question" widget to edit your question and provide better information.

To avoid repetitions, simply repeat getting the random value until you get a new unique value. How to quickly check up uniqueness on each step? Use the class System.Collections.Generic.HashSet<>:
https://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/bb356440%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/bb353005%28v=vs.110%29.aspx[^].

Repeat generation until Contains returns false, then Add the value to the hash set, and elsewhere, where you need the unique value.

—SA
 
Share this answer
 
Comments
Abhinav S 5-May-15 2:11am    
If the question is about generating a unique set from 1 to 35 then yes, a Hashset can be used.
Alternatively, a dictionary or a list will work equally well, so long as it is maintained by in this collection.

However, I don't think that is the question.
Sergey Alexandrovich Kryukov 5-May-15 2:20am    
Sorry, no. Please read the question. And it's not related to seeding. Right, HashSet would provide the solution...
—SA
Abhinav S 5-May-15 2:29am    
Well then in that case, I don't think this is what the user is looking for, but as long as one of the answers help him, it should be cool.
Sergey Alexandrovich Kryukov 5-May-15 2:33am    
I mean, using Random is trivial (and you demonstrated it already), the only less trivial thing is the uniqueness.
—SA
Lets say these names are all stored in an array 0..34.
Get them randomly as follows -
C#
Random r = new Random(Guid.NewGuid().GetHashCode());
Console.WriteLine(nameArray[r.Next(0, 35))];
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-May-15 2:02am    
Sorry, the main thing, non-repeated problem is not solved.
—SA
Abhinav S 5-May-15 2:10am    
The question is about generation of random numbers only. Seeding can be achieved by using GUIDs.
Sergey Alexandrovich Kryukov 5-May-15 2:20am    
No, sorry.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900