日期:2014-05-20 浏览次数:21051 次
MyClass[] myClassList
//设置顺序号以方便观察 MyClass[] myClassList = new MyClass[ 100 ]; for( int i = 0 ; i < myClassList.Length-1 ; i++ ) { myClassList[ i ] = new MyClass( i ); } //随机顺序 Random rd = new Random( Environment.TickCount ); for( int i = 0 ; i < myClassList.Length - 1 ; i++ ) { int x = rd.Next( 0, 99 ); int y = rd.Next( 0, 99 ); MyClass temp = myClassList[ x ]; myClassList[ x ] = myClassList[ y ]; myClassList[ y ] = temp; } MessageBox.Show( myClassList.Length.ToString() );
------解决方案--------------------
不知道有没有好的写法...代码补全了
class MyClass { public Guid MyGuid { get; set; } public int ID { get; set; } public MyClass(int i) { this.MyGuid = Guid.NewGuid(); this.ID = i; } } private void button1_Click( object sender, EventArgs e ) { //设置顺序号以方便观察 MyClass[] myClassList = new MyClass[ 100 ]; for( int i = 0 ; i < myClassList.Length-1 ; i++ ) { myClassList[ i ] = new MyClass( i ); } //随机顺序 Random rd = new Random( Environment.TickCount ); for( int i = 0 ; i < myClassList.Length - 1 ; i++ ) { int x = rd.Next( 0, 99 ); int y = rd.Next( 0, 99 ); MyClass temp = myClassList[ x ]; myClassList[ x ] = myClassList[ y ]; myClassList[ y ] = temp; }
------解决方案--------------------
List<MyClass> srcList = new List<MyClass>(myClassList);
List<MyClass> destList = new List<MyClass>(myClassList.Length);
System.Random rnd = new Random();
while (srcList.Count > 0)
{
int tmpIndex = rnd.Next(0, srcList.Count);
destList.Add(srcList[tmpIndex]);
srcList.RemoveAt(tmpIndex);
}
MyClass[] destMyClassList = destList.ToArray();
------解决方案--------------------
MyClass[] myClassList = ...; byte[] keys = new byte[myClassList.Length]; (new Random()).NextBytes(keys); Array.Sort(keys, myClassList);
------解决方案--------------------
//谢狼兄,又学到新内容了,陌生的概念啊,备注下
http://jonny131.javaeye.com/blog/188829
/* * 线性同余法求伪随机数的公式: * X(n+1) = (a Xn + c) mod m */ uint Rand( uint seed ) { return (uint)( seed * 1103515245 + 12345 ) % 32768; }
------解决方案--------------------