我有一個字符串列表,我想隨機從這個列表中選擇。當選擇一個字符串時,必須從列表中刪除。只有當所有的字符串被挑選出來後,列表中的纔會重新填充。我如何實現這一目標?從沒有替換的列表中選擇一個隨機字符串C#
0
A
回答
0
首先,製作一個隨機數發生器。
Random rand = new Random();
然後,生成一個數字並從列表中刪除該項目。我假設你正在使用System.Collections.Generic.List
。
int num = Random.Next(list.Count);
list.RemoveAt(num);
0
可以實現它非常簡單:
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);
}
}
}
}
,然後使用它:
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));
0
我想,你需要somethink喜歡隔壁班:
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;
}
}
你應該用下一個方法:
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());
}
相關問題
- 1. 從列表中選擇任意一個隨機字符串
- 2. 如何從列表中選擇一個隨機字符串
- 3. 從給定列表中選擇隨機字符串
- 4. 如何在字符串中隨機替換一個字符?
- 5. 如何隨機替換字符串列表中的
- 6. 如何從C#中的預寫字符串中隨機選擇?
- 7. 從數組列表中隨機打印一個沒有重複的字符串
- 8. 如何從數組中隨機選擇一個字符串
- 9. 用隨機字符替換字符串中的每個數字
- 10. 2從一個列表中隨機選擇 - C#
- 11. Python從字符串中有選擇地替換字符
- 12. Python:列表和隨機。如何從0到[列表中的字符串數量]之間選擇隨機數字?
- 13. 如何隨機選擇三個字符串中的一個?
- 14. 隨機選擇從列表
- 15. 從多維numpy陣列中選擇隨機陣列替換
- 16. 在java中隨機選擇字符串
- 17. 從netLogo中的字符串列表/數組中挑選一個隨機元素
- 18. 從一列中選擇隨機行但沒有重複的值
- 19. 替換字符串中的字符串列表(Objective-c 2.0)
- 20. 從字符串替換隨機數與正則表達式
- 21. 如何從列表中隨機選擇一個字符串,並將其插入到新列表中?
- 22. 如何在Android中隨機選擇一個字符串?
- 23. 從文件中替換包含隨機數的字符串
- 24. 從一個陣列替換字符到另一個字符串
- 25. C:從多字節字符串中選擇一個子字符串,n列寬
- 26. php中的隨機字符串列表
- 27. 根據隨機數字選擇一個字符串
- 28. 只是在列表中沒有選擇的情況下從列表中僞隨機選取一個元素
- 29. 從string.xml中挑選隨機字符串
- 30. 從Selenium Webdriver的列表中選擇一個隨機元素Java
隨機播放列表,然後逐個使用字符串。 –