Three implementation methods for asp.net random array sorting

Source: Internet
Author: User
Tags rand

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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.