2016-08-16 13 views
-2

私は5つの数字(exp。1 2 3 4 5)でいくつかのリストを持っています(ページリフレッシュ)例:(2 4 3 1 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:値のどれもが0されていない場合は、cellMapを削除することができます