1. random array ArrangementAlgorithmSimple Verification
2. Simple Random algorithm analysis
3.CodeDownload
1. random array Arrangement Algorithm and simple verification
1.1 The final purpose of the random Algorithm for Array arrangement is to achieve this. The probability of each element in the array appearing at each position is 1/N (where N is the array size ). An algorithm is as follows. For each element a [I] in array a, a random weight value is attached, and then the array a is sorted based on the weight value. The following is a simple implementation. The sorting uses Bubble sorting. The Code is as follows:
Public Static Void Permutebysorting ( Ref Int [])
{
Int Length = A. length;
List < Int > B =
New List < Int > ();
// Random Number
System. Random Rand = New System. Random ();
Foreach ( Int Item In A)
{
// [1, length ^ 3]
B. Add (RAND. Next ( 1 ,( Int ) Math. Pow (length, 3 )));
}
// Sort by key. Bubble Sorting is used here.
Int TMP;
For ( Int I = 0 ; I < B. count; ++ I)
For ( Int J = B. Count - 1 ; J > = (I + 1 ); -- J)
{
// Incremental, data exchange
If (B [I] > B [J])
{
// There is no need to record data in a and exchange data in.
TMP = A [I];
A [I] = A [J];
A [J] = TMP;
}
}
}
1.2 another random array algorithm is as follows: traverse the array, randomly select another element of the array, and exchange the positions of the two elements. The algorithm is relatively simple and the implementation code is as follows:
// Local sorting
Public Static Void Randomizeinplace ( Ref Int [])
{
Int Length = A. length;
System. Random Rand= NewSystem. Random ();
IntNum, TMP;
For ( Int I = 0 ; I < Length; ++ I)
{
Num = Rand. Next ( 0 , (Length - 1 ));
TMP = A [I];
A [I] = A [num];
A [num] = TMP;
}
// output result
foreach ( int item in )
{< br> console. writeline (item);
}< br>
}
2. Random algorithm analysis
An important analysis method in random algorithms is the indicator analysis method. The Defined variables are as follows:
Define sample space S
Event
Random Variable indicator I
I (A) is defined as follows:
A simple example of using a random indicator variable: we all know that if the coin is even, the number of times the front may appear n/2 when the coin is flushed n times, that is to say, the number of positive appearance and the number of negative appearance are the same in a large number of statistics. Next we will prove this fact through mathematical methods.
Define the random variable X to indicate the number of times the front of the coin is thrown n times. The event can be broken down into the following Xi, indicating that the front of the first coin is thrown up. Therefore
Expected values for both sides:
That is to say, in the project where n coins are flushed, the number of front-up times may be n/2. Through the analysis of the above example, we can see that the most important thing is to define event X, then break down event X into a minor event XI, and finally obtain the expected value of X. The above method proves the "birthday paradox" and other problems.
3. Download Code
/Files/xuqiang/random.rar