1

Good day I have some problem regarding selecting a random string from my string array I am currently developing a guessing word game. this is my string array:

 string[] movie = {"deadpool", "batmanvssuperman", "findingdory", "titanic", "suicidesquad", "lordoftherings", "harrypotter", "jurassicpark", "hungergames", "despicableme" };

while this is the process in selecting a random string to my array, what should i do next, because I want to select the string not repeated. e.g when the program starts it will select a string then when i select random string again i want to not select the previous word that i've already selected previously.

string word = movie[r.Next(0, movie.Length)].ToUpper();

Thank you for response! Have a nice day.

1
  • The classical approach is to create a shuffled array, rather than pick at random from the original array., Commented Sep 22, 2016 at 14:36

3 Answers 3

2

Well, simply convert your array to list and shuffle it in random order :

        var rand = new Random();
        string[] movies = { "deadpool", "batmanvssuperman", "findingdory", "titanic", "suicidesquad", "lordoftherings", "harrypotter", "jurassicpark", "hungergames", "despicableme" };
        List<string> randomMovies = movies.ToList();

        for (int i = 0; i < movies.Length / 2; i++)
        {
            var randNum = rand.Next(i, randomMovies.Count);
            var temp = randomMovies[randNum];
            randomMovies[randNum] = randomMovies[i];
            randomMovies[i] = temp;
        }

Then you can just take random elements by :

var randomMovie = randomMovies.First(); 
randomMovies.Remove(randomMovie); // either remove it or use loop to iterate through the list

I sort of like to use Queue collection here :

var moviesQueue = new Queue<string>(randomMovies);    

while (moviewQueue.Count > 0)
{
    Console.WriteLine(moviewQueue.Dequeue());
}

P.S. As suggested you don't really need to delete elements from randomMovie, you can save last used index in some field and use it later;

var lastIndex = 0;
var randomMovie = randomMovies[lastIndex++];
Sign up to request clarification or add additional context in comments.

3 Comments

This has the side effect or destroying your list once you are finished.
But you can always copy list if you need with help of randomMovies .ToList() method
Best answer, but you don't need to remove elements. You could just keep an index pointer around, something like int CurrentMovieIdx.
1

Just loop if it's been selected. This is untested code:

private string _last;
private string GetNonRepeatedMovie()
{
    string selected = "";
    do
    {
        selected = movie[r.Next(0, movie.Length)].ToUpper();
    }
    while (selected == this._last);

    this._last = selected;
    return selected;    
}

This should work to select the initial string as well when the application starts.

6 Comments

With this method there is a very small chance that you could loop for an unacceptably long time before selecting something else. I wouldn't worry about that though.
This will make sure the current choice isn't the last, but has no ability to make sure each choice is unique.
Its more likely that you win a million in a lottery than that the user will notice the method-call at all. You just need to handle the case that there is none or only one movy ;-)
@NicholausLawson: true, but it seems that OP only wants to avoid that a movie is repeated immediately. Then this is the most efficient and easiest approach:
@TimSchmelter yeah, very possibly. Just saying 'previously selected' could go either way lol. Just pointing out. incase they meant selection without replacement or just avoid drawing the same back-to-back
|
0

If you need to keep a memory, convert your list to be a class that contains the name and a field of whether it has been chosen or not.

If you go through all of them, turn this semiphor off and begin again.

class GuessingName
{
  public GuessingName(string name){Name = name;}

  public string Name;
  public bool chosen;
}

class RandomNamePicker{
 private List<GuessingName> names;

 public RandomNamePicker(){
  names = new List<GuessingName>();
  names.Add(new GuessingName("movie"));
 }

 string RandomPicker(){

  if(names.All(c=>c.chosen))
    names.ForEach(c=>c.chosen=false);

  int r1 = r.Next(0, names.Length);

  while(names[r1].chosen){
   r1= r.Next(0,names.Length);
  }
  return names[r1].Name;
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.