从名称数组中随机选取的示例名称生成器
本文关键字:选取 数组 随机 | 更新日期: 2023-09-27 17:59:21
是否存在从名称数组中随机选择的linq随机选择器?
var randomname = {"Jess", "Jay", "Jen", "Jack", "Jan"}.select(a => a.random());
简单,只需使用随机数生成器并对其进行排序:
Random rnd = new Random();
var randomname = {"Jess", "Jay", "Jen", "Jack", "Jan"}.OrderBy(a => rnd.NextDouble()).First();
此处不需要LINQ。只需使用具有上限和下限的Random.Next
:
string[] items = { "Jess", "Jay", "Jen", "Jack", "Jan" };
Random r = new Random();
int index = r.Next(0, items.Length);
string randomItem = items[index];
示例:http://dotnetfiddle.net/EiRwGl
您可以使用.skip
Random rand = new Random();
int toSkip = rand.Next(0, randomname.Count );
randomname.Skip(toSkip).Take(1).First(); //Or without linq a randomname[toSkipe];