C#实现随机洗牌 C#实现随机洗牌的方法
apollokk 人气:0想了解C#实现随机洗牌的方法的相关内容吗,apollokk在本文为您仔细讲解C#实现随机洗牌的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,随机,洗牌,方法,下面大家一起来学习吧。
本文实例讲述了C#实现随机洗牌的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
#region 随机洗牌
int[] ints = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
List<int> list=ints.ToList();
int[] outs = new int[20];
Random rand = new Random();
for (int i = 0; i < 20; i++)
{
int x = rand.Next(list.Count);
outs[i] = list[x];
list.RemoveAt(x);
}
Response.Write("<hr/>");
foreach (int i in outs)
{
Response.Write(i.ToString() + " ");
}
#endregion
int[] ints = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
List<int> list=ints.ToList();
int[] outs = new int[20];
Random rand = new Random();
for (int i = 0; i < 20; i++)
{
int x = rand.Next(list.Count);
outs[i] = list[x];
list.RemoveAt(x);
}
Response.Write("<hr/>");
foreach (int i in outs)
{
Response.Write(i.ToString() + " ");
}
#endregion
希望本文所述对大家的C#程序设计有所帮助。
加载全部内容