List<int> list = new List<int>(); Random rnd = new Random(); while (list.Count < 5) { int n = rnd.Next(1, 41); if (!list.Any(x => x == n)) list.Add(n); }
本帖最后由 caozhy 于 2011-06-27 00:40:00 编辑
扑克牌 洗牌算法!先把数字随机排。然后顺序取。 int temp; int index; Random rnd = new Random(); for(int i=0;i<arr.length;i++){ temp = arr[i]; index= rnd.Next(0, arr.length); arr[i] = arr[index]; arr[index] = temp; }
List<int> list = new List<int>(); Random ran = new Random(); for (int i = 1; i <= 40; i++) list.Add(i); while (list.Count >= 8) { for (int i = 0; i < 8; i++) { int temp = list[ran.Next(0, list.Count)]; Console.Write(temp.ToString().PadLeft(2, '0') + " "); list.Remove(temp); } Console.WriteLine(); } Console.ReadLine(); /* 26 25 11 24 36 10 31 35 13 17 21 34 04 38 39 06 32 02 27 01 40 29 07 09 03 28 15 22 30 05 16 19 14 18 23 33 37 20 12 08 */
Random rnd = new Random();
while (list.Count < 5)
{
int n = rnd.Next(1, 41);
if (!list.Any(x => x == n)) list.Add(n);
}
int temp;
int index;
Random rnd = new Random();
for(int i=0;i<arr.length;i++){
temp = arr[i];
index= rnd.Next(0, arr.length);
arr[i] = arr[index];
arr[index] = temp;
}
Random ran = new Random();
for (int i = 1; i <= 40; i++)
list.Add(i);
while (list.Count >= 8)
{
for (int i = 0; i < 8; i++)
{
int temp = list[ran.Next(0, list.Count)];
Console.Write(temp.ToString().PadLeft(2, '0') + " ");
list.Remove(temp);
}
Console.WriteLine();
}
Console.ReadLine();
/*
26 25 11 24 36 10 31 35
13 17 21 34 04 38 39 06
32 02 27 01 40 29 07 09
03 28 15 22 30 05 16 19
14 18 23 33 37 20 12 08 */