I have a list of strings and I want to randomly pick from this list. When a string is picked, it must be removed from the list. Only when all the strings have been picked then the list is repopulated. How do I achieve this?
Asked
Active
Viewed 233 times
0
-
Shuffle the list, then use the strings one-by-one. – Hans Kesting Nov 26 '16 at 12:33
3 Answers
0
First, make a random number generator.
Random rand = new Random();
Then, generate a number and remove the item from the list. I'm assuming you're using System.Collections.Generic.List
.
int num = Random.Next(list.Count);
list.RemoveAt(num);

devRicher
- 428
- 1
- 7
- 21
0
You can implement it quite straightforward:
public static partial class EnumerableExtensions {
public static IEnumerable<T> RandomItemNoReplacement<T>(this IEnumerable<T> source,
Random random) {
if (null == source)
throw new ArgumentNullException("source");
else if (null == random)
throw new ArgumentNullException("random");
List<T> buffer = new List<T>(source);
if (buffer.Count <= 0)
throw new ArgumentOutOfRangeException("source");
List<T> urn = new List<T>(buffer.Count);
while (true) {
urn.AddRange(buffer);
while (urn.Any()) {
int index = random.Next(urn.Count);
yield return urn[index];
urn.RemoveAt(index);
}
}
}
}
And then use it:
private static Random gen = new Random();
...
var result = new List<string>() {"A", "B", "C", "D"}
.RandomItemNoReplacement(gen)
.Take(10);
// D, C, B, A, C, A, B, D, A, C (seed == 123)
Console.Write(string.Join(", ", result));

Dmitry Bychenko
- 180,369
- 20
- 160
- 215
0
I think, you need somethink like next class:
public class RandomlyPickedWord
{
public IList<string> SourceWords{ get; set; }
protected IList<string> Words{ get; set; }
public RandomlyPickedWord(IList<string> sourceWords)
{
SourceWords = sourceWords;
}
protected IList<string> RepopulateWords(IList<string> sources)
{
Random randomizer = new Random ();
IList<string> returnedValue;
returnedValue = new List<string> ();
for (int i = 0; i != sources.Count; i++)
{
returnedValue.Add (sources [randomizer.Next (sources.Count - 1)]);
}
return returnedValue;
}
public string GetPickedWord()
{
Random randomizer = new Random ();
int curIndex;
string returnedValue;
if ((Words == null) || (Words.Any () == false))
{
Words = RepopulateWords (SourceWords);
}
curIndex = randomizer.Next (Words.Count);
returnedValue = Words [curIndex];
Words.RemoveAt (curIndex);
return returnedValue;
}
}
Which you should use by the next way:
IList<string> source = new List<string> ();
source.Add ("aaa");
source.Add ("bbb");
source.Add ("ccc");
RandomlyPickedWord rndPickedWord = new RandomlyPickedWord (source);
for (int i = 0; i != 6; i++)
{
Console.WriteLine (rndPickedWord.GetPickedWord ());
}

Mixim
- 972
- 1
- 11
- 37