【发布时间】:2017-04-19 05:32:21
【问题描述】:
这是我的问题函数,我将数据集合发送到 ShuffledList 函数以便一次获取一行
private void nextQuestion_Question()
{
ql1 = ShuffleList(ql1);
currentQuestion_List = ql1.ElementAt(0);
//ql1.RemoveAt(0);
txtQ.Text = currentQuestion_List.text;
btnA.Text = currentQuestion_List.a;
btnB.Text = currentQuestion_List.b;
btnC.Text = currentQuestion_List.c;
btnD.Text = currentQuestion_List.d;
}
但这个函数有时会返回重复的对象
private List<E> ShuffleList<E>(List<E> inputList)
{
List<E> randomList = new List<E>();
Random r = new Random();
int randomIndex = 0;
while (inputList.Count>0)
{
randomIndex = r.Next(inputList.Count); //Choose a random object in the list
randomList.Add(inputList[randomIndex]); //add it to the new, random list
inputList.RemoveAt(randomIndex); //remove to avoid duplicates
}
return randomList; //return the new random list
}
请注意,我正在尝试制作一个多选游戏,它运行良好,但有时会重复相同的问题 请帮我解决这个问题
【问题讨论】:
-
您的 ShuffleList 有缺陷。它作为就地 suffle 交换元素更有效;您应该在应用程序的生命周期中使用相同的随机对象;适当的 shuffle 向后循环,以便每个元素仅交换一次。然后每局只洗一次
-
我也在尝试按照你说的做,但它对我不起作用可以纠正吗?
-
这里有很多很多答案展示了如何正确地洗牌