-2

i have some list with 5 numbers (exp. 1 2 3 4 5) i want to order them in random ordering each time (page refresh) examples: (2 4 3 1 5) (1 3 5 4 2) (5 1 2 3 4)... code in C#, Thanks

var loadcards = (from card in db.GameCards
                     select card).Take(5).ToList();

    foreach (var item in loadcards)
    {
        Response.Write("<script>alert('" + item.cardId + "');</script>");
    }
Elidotnet
  • 291
  • 3
  • 18

1 Answers1

0

Something like this:

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: You can remove the cellMap if none of the values is 0

Pau C
  • 773
  • 4
  • 20