c# - select randomly from string array without repetitions -


good day have problem regarding selecting random string string array developing guessing word game. this string array:

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

while process in selecting random string array, should next, because want select string not repeated. e.g when program starts select string when select random string again want not select previous word i've selected previously.

string word = movie[r.next(0, movie.length)].toupper(); 

thank response! have nice day.

well, convert array list , shuffle in random order :

        var rand = new random();         string[] movies = { "deadpool", "batmanvssuperman", "findingdory", "titanic", "suicidesquad", "lordoftherings", "harrypotter", "jurassicpark", "hungergames", "despicableme" };         list<string> randommovies = movies.tolist();          (int = 0; < movies.length / 2; i++)         {             var randnum = rand.next(i, randommovies.count);             var temp = randommovies[randnum];             randommovies[randnum] = randommovies[i];             randommovies[i] = temp;         } 

then can take random elements :

var randommovie = randommovies.first();  randommovies.remove(randommovie); // either remove or use loop iterate through list 

i sort of use queue collection here :

var moviesqueue = new queue<string>(randommovies);      while (moviewqueue.count > 0) {     console.writeline(moviewqueue.dequeue()); } 

p.s. suggested don't need delete elements randommovie, can save last used index in field , use later;

var lastindex = 0; var randommovie = randommovies[lastindex++]; 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -