We have summarized three methods for random sorting of array elements. The following three examples can be selected based on your needs.
This is usually the case for four people. Take "a" as an example. There is a middle shift every day, an early shift and an evening shift every two days, and a rest every three days. The middle shift is continued every four days. In this way, three people are on duty, and the other one is on vacation for a month. The other two are on duty after the shift.
The following is a random Shift Algorithm for your reference only.
// Shift by sequence. y indicates the date and x indicates the shift.
The Code is as follows: |
Copy code |
String str = new string {"Zhang", "Wang", "Zhao", "Li "}; Int B = 0; String abc = new string; For (int y = 0; y <30; y ++) { For (int x = 0; x <3; x ++) { Abc = str; B ++; } } // The following random sorting ensures that the number of employees on duty remains unchanged. Random ran = new Random (); String temp; Int x1, y1, x2, y2; For (int x = 0; x <100; x ++) { X1 = ran. Next (2 ); X2 = ran. Next (2 ); Y1 = ran. Next (29 ); Y2 = ran. Next (29 ); Temp = abc; Abc = abc; Abc = temp; } |
Instance 2
Each time the array is queried, the data sequence in the array is different.
The Code is as follows: |
Copy code |
Int [] array = new int [10]; For (int I = 0; I <10; I ++) { Array [I] = I + 1; } Int len = array. Length; Random rnd = new Random (); For (int j = 0; j <len; j ++) { Int pos = rnd. Next (len ); Int temp = array [pos]; Array [pos] = array [j]; Array [j] = temp; } String sortStr = ""; For (int I = 0; I <len; I ++) { SortStr + = array [I] + ","; } MessageBox. Show (sortStr ); |
Random sorting of arrays, that is, to disrupt the order of elements in an array, such as shuffling.
The Code is as follows: |
Copy code |
Public static T [] RandomSort <T> (T [] array) ...{ Int len = array. Length; List <int> list = new List <int> (); T [] ret = new T [len]; Random rand = new Random (); Int I = 0; While (list. Count <len) ...{ Int iter = rand. Next (0, len ); If (! List. Contains (iter )) ...{ List. Add (iter ); Ret [I] = array [iter]; I ++; }
} Return ret; } Call example: Static void Main () ...{ String [] aaa =... {"3", "1", "7", "5", "4", "2", "6 "}; String [] bbb = RandomSort (aaa ); For (int I = 0; I <7; I ++) Console. WriteLine (bbb [I]); Console. ReadLine (); } |