Shuffle list with some conditions

后端 未结 4 1227
余生分开走
余生分开走 2021-01-04 22:51

I have a list of elements which can be easily compared using Equals(). I have to shuffle the list, but the shuffle must satisfy one condition:

The i\'th

4条回答
  •  独厮守ぢ
    2021-01-04 23:24

    Your requirements eliminate the real shuffling alternative: there is no randomness, or there is controlled randomness. Here is one special approach

    1. Sort the original list L
    2. Find the mode M, and its frequency n
    3. If n is even, n++.
    4. Create k (= 5*n - 1) lists (prime to n, and 5 times n) L1 through Lk
      (5 is chosen to avoid two previous elements and two next elements)
    5. Assign all elements into the k lists in round robin fashion
    6. Individually shuffle all k lists.
    7. Collate the contents of k lists in the following order: a. pick randomly +5 or -5 as x.
      b. pick a random number j.
      c. repeat k times:
      i. add all contents from Lj.
      ii. j <- (j + x) mod k

    [5, 6, 7, 7, 8, 8, 9, 10, 12, 13, 13, 14, 14, 14, 17, 18, 18, 19, 19, 20, 21, 21, 21, 21, 24, 24, 26, 26, 26, 27, 27, 27, 29, 29, 30, 31, 31, 31, 31, 32, 32, 32, 33, 35, 35, 37, 38, 39, 40, 42, 43, 44, 44, 46, 46, 47, 48, 50, 50, 50, 50, 51, 52, 53, 54, 55, 56, 57, 57, 58, 60, 60, 60, 61, 62, 63, 63, 64, 64, 65, 65, 65, 68, 71, 71, 72, 72, 73, 74, 74, 74, 74, 75, 76, 76, 76, 77, 77, 77, 78, 78, 78, 79, 79, 80, 81, 82, 86, 88, 88, 89, 89, 90, 91, 92, 92, 92, 93, 93, 94, 94, 95, 96, 99, 99, 100, 102, 102, 103, 103, 105, 106, 106, 107, 108, 113, 115, 116, 118, 119, 123, 124, 125, 127, 127, 127, 128, 131, 133, 133, 134, 135, 135, 135, 137, 137, 137, 138, 139, 141, 143, 143, 143, 145, 146, 147, 153, 156, 157, 158, 160, 164, 166, 170, 173, 175, 181, 181, 184, 185, 187, 188, 190, 200, 200, 215, 217, 234, 238, 240]

    Frequency of mode = 4, so 19 slots (#0 - #18)

    0: [7, 21, 32, 50, 65, 77, 93, 115, 137, 173]
    1: [8, 21, 33, 51, 65, 78, 93, 116, 137, 175]
    2: [8, 24, 35, 52, 65, 78, 94, 118, 138, 181]
    3: [9, 24, 35, 53, 68, 78, 94, 119, 139, 181]
    4: [10, 26, 37, 54, 71, 79, 95, 123, 141, 184]
    5: [12, 26, 38, 55, 71, 79, 96, 124, 143, 185]
    6: [13, 26, 39, 56, 72, 80, 99, 125, 143, 187]
    7: [13, 27, 40, 57, 72, 81, 99, 127, 143, 188]
    8: [14, 27, 42, 57, 73, 82, 100, 127, 145, 190]
    9: [14, 27, 43, 58, 74, 86, 102, 127, 146, 200]
    10: [14, 29, 44, 60, 74, 88, 102, 128, 147, 200]
    11: [17, 29, 44, 60, 74, 88, 103, 131, 153, 215]
    12: [18, 30, 46, 60, 74, 89, 103, 133, 156, 217]
    13: [18, 31, 46, 61, 75, 89, 105, 133, 157, 234]
    14: [19, 31, 47, 62, 76, 90, 106, 134, 158, 238]
    15: [19, 31, 48, 63, 76, 91, 106, 135, 160, 240]
    16: [5, 20, 31, 50, 63, 76, 92, 107, 135, 164]
    17: [6, 21, 32, 50, 64, 77, 92, 108, 135, 166]
    18: [7, 21, 32, 50, 64, 77, 92, 113, 137, 170]

    Shuffling individual lists, and picking lists 5 slots apart (start randomly at #16):

    16: [31, 135, 92, 76, 107, 5, 164, 63, 20, 50]
    2: [52, 24, 35, 78, 181, 8, 138, 94, 118, 65]
    7: [57, 143, 99, 81, 40, 13, 127, 72, 188, 27]
    12: [46, 30, 60, 89, 133, 74, 156, 18, 103, 217]
    17: [64, 50, 135, 92, 21, 32, 108, 77, 166, 6]
    3: [9, 94, 181, 119, 24, 35, 139, 68, 53, 78]
    8: [145, 27, 14, 57, 42, 100, 190, 82, 73, 127]
    13: [89, 18, 75, 61, 157, 234, 133, 105, 31, 46]
    18: [113, 21, 7, 92, 64, 32, 137, 50, 170, 77]
    4: [71, 10, 37, 26, 123, 54, 184, 79, 95, 141]
    9: [27, 74, 86, 14, 102, 146, 127, 43, 58, 200]
    14: [62, 106, 158, 134, 19, 47, 238, 31, 76, 90]
    0: [7, 77, 65, 21, 50, 93, 173, 115, 32, 137]
    5: [96, 79, 26, 185, 12, 71, 124, 143, 55, 38]
    10: [29, 14, 147, 60, 128, 88, 74, 44, 102, 200]
    15: [106, 240, 63, 48, 91, 19, 160, 31, 76, 135]
    1: [65, 33, 21, 51, 137, 8, 175, 93, 116, 78]
    6: [143, 26, 13, 56, 99, 72, 39, 80, 187, 125]
    11: [103, 88, 29, 60, 74, 44, 17, 153, 131, 215]

    [31, 135, 92, 76, 107, 5, 164, 63, 20, 50, 52, 24, 35, 78, 181, 8, 138, 94, 118, 65, 57, 143, 99, 81, 40, 13, 127, 72, 188, 27, 46, 30, 60, 89, 133, 74, 156, 18, 103, 217, 64, 50, 135, 92, 21, 32, 108, 77, 166, 6, 9, 94, 181, 119, 24, 35, 139, 68, 53, 78, 145, 27, 14, 57, 42, 100, 190, 82, 73, 127, 89, 18, 75, 61, 157, 234, 133, 105, 31, 46, 113, 21, 7, 92, 64, 32, 137, 50, 170, 77, 71, 10, 37, 26, 123, 54, 184, 79, 95, 141, 27, 74, 86, 14, 102, 146, 127, 43, 58, 200, 62, 106, 158, 134, 19, 47, 238, 31, 76, 90, 7, 77, 65, 21, 50, 93, 173, 115, 32, 137, 96, 79, 26, 185, 12, 71, 124, 143, 55, 38, 29, 14, 147, 60, 128, 88, 74, 44, 102, 200, 106, 240, 63, 48, 91, 19, 160, 31, 76, 135, 65, 33, 21, 51, 137, 8, 175, 93, 116, 78, 143, 26, 13, 56, 99, 72, 39, 80, 187, 125, 103, 88, 29, 60, 74, 44, 17, 153, 131, 215]

提交回复
热议问题