随机排序数字

本文关键字:数字 排序 随机 | 更新日期: 2023-09-27 18:02:35

我有一些列表与5个数字(exp. 1 2 3 4 5)我想在随机排序它们每次(页面刷新)的例子:(2 4 3 1 5)(1 3 5 4 2)(5 1 2 3 4 4)…c#代码,谢谢

var loadcards = (from card in db.GameCards
                     select card).Take(5).ToList();
    foreach (var item in loadcards)
    {
        Response.Write("<script>alert('" + item.cardId + "');</script>");
    }

随机排序数字

像这样:

int[] RandomizeOrder(int[] input)
{
 Random RNG = new Random();
 bool[] cellMap = new bool[input.Length];
 int[] output = new int[input.Length];
 for(int i = 0; i < input.Length; i++)
 {
  int index = RNG.Next(input.Length)
  while(cellMap[index)
   index = RNG.Next(input.Length);
  cellMap[index] = true;
  output[index] = input[i];      
 }
return output;
}

PS:如果没有值为0,则可以删除cellMap