Randomly assign the best position, efficient algorithm
Crazy pictures archives
Randomly assign the best position, efficient algorithm
Pi, Board, School, District, Diameter

Assumption: Now there are 100 candidates whose numbers are 1-100

The examination room is now 10 * 10 (10 people per row)

Now it is required that the seat number is randomly assigned, not fixed from row 1 to 100



I have already figured it out, but the efficiency is relatively low, and the above is just an example, is there any other good method, please ask!



1. Fill 1->100 into the linked list data structure R such as arraylist

2. Let N=100,

3. Take new Random().Next(1, N) to get the result n

4. Remove the element r at position n in R, the element r is the assigned position,

5. In the matrix corresponding to r to 10*10, get the assigned position

6.N--

7. Repeat step 2 until N=0, that is, all positions are allocated



correct:

. Take new Random().Next(1, N) to get the result n

Is there a high probability that the result n obtained in this step will appear the same?

-----------------

Very high,

For example, if you get 1 for the first time, the first element of the arraylist will be removed, which is 1, and the seat number is 1, 1.

The second time it gets 1 again, the first element of the arraylist is removed, namely 2, the seat number is 2, 1

such....