首先获得原始的题目列表
定义为List<Question> originQuestionList = 赋值;
定义一个最终的随机题目列表 List<Question> finalQuestionList = new List<Question>();
定义一个中间量用于记录已经选了哪些题目,这里用HashSet<int> IDList = new HashSet<int>();
定义一个随机量 Random rd = new Random();
HashSet类有Add()方法,当HashSet值集中已经包含这个值则返回false,没有则添加至该值集并放回false。
算法:
while(IDList.count<originQuestionList .count)
{
int idtoadd = rd.next(0,originQuestionList .count) ;
if(IDList .Add(idtoadd ))
{
finalQuestionList .add(originQuestionList (idtoadd ));
}
}
本文介绍了一种从原始题目列表中随机选择题目的算法实现过程。通过使用HashSet来记录已选题目ID,确保每个题目仅被选取一次。算法利用Random生成随机数,并检查该题目是否已被选择,若未被选择则加入最终题目列表。
938

被折叠的 条评论
为什么被折叠?



