C#實現隨機洗牌的方法
更新時間:2015年02月16日 15:11:37 作者:apollokk
這篇文章主要介紹了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#程序設計有所幫助。
相關文章
在WCF數據訪問中使用緩存提高Winform字段中文顯示速度的方法
這篇文章主要介紹了在WCF數據訪問中使用緩存提高Winform字段中文顯示速度的方法,是非常實用的功能,需要的朋友可以參考下2014-09-09

C#靜態(tài)代碼織入AOP組件之Rougamo的使用詳解
Rougamo是一個靜態(tài)代碼織入的AOP組件,同為AOP組件較為常用的有Castle、Autofac、AspectCore等,下面就跟隨小編一起來學習一下它的具體使用吧
2024-01-01