2016-08-16 100 views
-2

我有一些列表有5个数字(例如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>"); 
    } 
+0

那么你有什么尝试? – KDecker

+0

'.OrderBy(a => card.Next())'? – Adam

回答

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