Random method
-
Trying to use the random() to generate random words from a string[],now I want to put the already picked words in may be an array so that the random() should not pick that word if once picked before.please help
-
Trying to use the random() to generate random words from a string[],now I want to put the already picked words in may be an array so that the random() should not pick that word if once picked before.please help
If you do that, you have to loop when you try to find an unused word. Instead, put the words in a list, and remove the words that are picked:
// create the list
List<string> words = new List<string>(arrayOfWords);
// create a random object to use
Random rnd = new Random();// pick one item from the list
int index = rnd.Next(words.Count);
// get the string
string pickedWord = words[index];
// remove the picked item from the list
words.RemoveAt(index);Despite everything, the person most likely to be fooling you next is yourself.
modified on Sunday, September 7, 2008 11:17 AM
-
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.
Thats was really a great prog. Now check here, am generating a random word from an array which I display on the textbox in form of stars for the user to guess.Now I don't want the user to come across the same word again.How can I please go about it to make the random method not to get the same word again.Like the way Hung man game operates waiting......
-
If you do that, you have to loop when you try to find an unused word. Instead, put the words in a list, and remove the words that are picked:
// create the list
List<string> words = new List<string>(arrayOfWords);
// create a random object to use
Random rnd = new Random();// pick one item from the list
int index = rnd.Next(words.Count);
// get the string
string pickedWord = words[index];
// remove the picked item from the list
words.RemoveAt(index);Despite everything, the person most likely to be fooling you next is yourself.
modified on Sunday, September 7, 2008 11:17 AM
-
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.
-
That was quiet penetrating, which namespace do I use for Lists. Is it an ArrayList or SortedList you are talking about(am familiar with these System.Collections;, System.Text;)? :((
-
The List<T> class is in the
System.Collections.Generic
namespace. Unless you are stuck with framework 1.x you should not useArrayList
at all.Despite everything, the person most likely to be fooling you next is yourself.
-
Thanks. Unfortunatly, am only able to program on one form.How about having multiple forms and calling them interchangebly.
MorganSim wrote:
Unfortunatly, am only able to program on one form.How about having multiple forms and calling them interchangebly.
I don't see how this relates to the original question at all? Can you clearify?
Despite everything, the person most likely to be fooling you next is yourself.