随机取题的实现方法

可以通过将考题的ID取出保存进一个数组中,然后对数组打乱顺序,即可实现随机出题。

代码如下:
  

 1    /// <summary>
 2        /// 打乱QuestionID的排列方式
 3        /// </summary>
 4        /// <returns></returns>

 5        public string[] MakeRandomString()
 6        {
 7            DataTable dtExam = (DataTable)Cache["ExamTable"];
 8            string[] strNumber = new string[dtExam.Rows.Count];
 9            int i = 0;
10            
11            foreach(DataRow dr in dtExam.Rows)
12            {
13                strNumber[i] += dr["ID"].ToString();
14                i++;
15            }

16            int arrLength = strNumber.Length;
17            System.Random rndNumber = new System.Random();
18            for (int j = 0;j < arrLength * 10; j++)
19            {
20                //生成两个随机数
21                int rndInt1 = rndNumber.Next(arrLength);
22                int rndInt2 = rndNumber.Next(arrLength);
23                //将两个数组元素位置对换。
24                string tmp = strNumber[rndInt1];
25                strNumber[rndInt1] = strNumber[rndInt2];
26                strNumber[rndInt2] = tmp;
27            }

28            return strNumber;
29        }
posted @ 2006-11-24 10:11  祝金峰  阅读(337)  评论(0编辑  收藏  举报