As a study of the slag of mathematics, in fact, the various algorithms are still interested. Sometimes it's better to see a great God's ingenious algorithm than a concert.
Shuffle algorithm is often used to look like repeatedly, love to eliminate this small game. The aim is to disrupt the established order.
I now like and feel that efficient and easy to understand the shuffling algorithm has two ways to achieve. The following one by one statements.
Benbow in csdn to see a great God wrote, readily excerpt.
The first is to use the method of taking the remainder randomly to take the value
Private voidRANDOM1 (int[] Array,intlength) { intindex; intvalue; intWangsai; if(Array = =NULL|| Length = =0) { return; } for(index =0; Index < length; index++) { //the random number is to take the remainderValue = index +NewRandom (). Next (0,10000000)% (Length-index); Median=Array[index]; Array[index]=Array[value]; Array[value]=Wangsai; } }
The second method is to take a random index value
Private voidRandom2 (int[] Array,intlength) { intindex; intvalue; for(inti = length-1; i >0; i--) {Index=NewRandom (). Next (0, i+1); Value=Array[i]; Array[i]=Array[index]; Array[index]=value; } }
Specific implementation scenarios:
Public Static voidMain (string[] args) { int[] numbers =New int[ Wu]; intLength =numbers. Length; for(inti =0; i < length; i++) {Numbers[i]= i +1; } MainClass M=NewMainClass (); M.RANDOM1 (numbers, length); foreach(intIinchnumbers) {Console.Write (i+" "); } Console.WriteLine ("**********************"); M.RANDOM2 (numbers, length); foreach(intIinchnumbers) {Console.Write (i+" "); }
Trapped Hungry hungry ... No code word!!
Shuffling algorithm of C #----algorithm