随机排序数字

时间:2016-08-16 18:23:08

标签: c# linq

我有一些带有5个数字的列表(exp.1 2 3 4 5)我想每次以随机顺序订购它们(页面刷新)示例:(2 4 3 1 5)(1 3 5 4 2)( 5 1 2 3 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>");
    }

1 个答案:

答案 0 :(得分:0)

这样的事情:

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:如果没有cellMap

值,您可以删除0
相关问题