mercredi 27 juin 2018

Pick random values from c# array and make sure you don't use the same one again [duplicate]

This question already has an answer here:

I wanted to pick random values from a c# array of strings in a foreach loop and make sure that i don't pick the same value twice...

I know how to build the array and loop through it, but I don't know how to randomly pick a value from the array or keep track of the values I've already picked, can someone enlighten me please?

So far I have;

//create an array of strings
string[] stringArray = { "string 1", "string 2", "string 3"};

//loop through the array using the strings in a foreach loop
foreach (string randomStringSelected in stringArray)
{
//do stuff here with randomly selecting value from array HERE
}

....pretty standard, but it would be nice to randomly pick "string 2" first, keep a track of the fact I've used it, on the next loop around in the foreach pick either "string 1" or "string 3"...but I don't know how. I've seen something about using a List or Dictionary and the Random function but (if I'm being honest) I didn't understand how to get those things worked.

Many thanks in advance for any help!




Aucun commentaire:

Enregistrer un commentaire