是否存在从名称数组中随机选择的linq随机选择器?
var randomname = {"Jess", "Jay", "Jen", "Jack", "Jan"}.select(a => a.random());
答案 0 :(得分:1)
简单,只需使用随机数生成器并对其进行排序:
Random rnd = new Random();
var randomname = {"Jess", "Jay", "Jen", "Jack", "Jan"}.OrderBy(a => rnd.NextDouble()).First();
答案 1 :(得分:1)
这里不需要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];
答案 2 :(得分:1)
您可以使用.skip
Random rand = new Random();
int toSkip = rand.Next(0, randomname.Count );
randomname.Skip(toSkip).Take(1).First(); //Or without linq a randomname[toSkipe];