algorithm - Shuffle list, ensuring that no item remains in same position -


I want to revoke the list of unique items, but do not want to completely shuffle randomly. I need to make sure that no element in the ugly list is in the same place in the original list. Thus, if the original list is (A, B, C, D, E), then the result would be fine: (C, D, B, E, A), but this would not be: (C, E, A, D , B) Because "D" is still the fourth item. There are seven items in the list. Extreme efficiency is not an idea. I think that Fischer / Yets performs this modification, but I can not prove it mathematical:

  function shuffle (data) {for (var i = 0; i & lt; data.length - 1; i ++) {var j = i + 1 + Math.flur (Math.Rendum ( ) * (Data length - i - 1)); Var temp = data [j]; Data [ja] = data [i]; Data [i] = temporary; }}    

You are searching one of your listings. First of all, your algorithm works in this sense that it represents a random satire, i.e. a permutation with no fixed point, though it is a huge defect (which you do not mind, But it is worth keeping in mind): Some hiccups can not be obtained from your algorithm In other words, it gives zero probability for some potential derangements The resultant distribution is definitely not equally random.

A possible solution, as suggested in the comments, will be to use the rejection algorithm:

  • Choose a permutation evenly on the random
  • If it is not a fixed number, return it
  • Try again

    Asymptotically, the probability of receiving a derangement 1 / e = 0.3679 (as seen in the Wikipedia article). Which means that to achieve a rush, you have to generate an average e = 2.718 permutation, which is quite expensive.

    A better way to do this will be to reject at CD / CD-ROM at each stage of the AM algorithm, assuming something like the original code i At i i.e. a [i] == i ):

     for  (i = 1 to n-1) {do {J = Random (i, n) // is the random integer i from n inclusive) while a [j]! = I // Disclaimer Your Swap One [i] a [j]}   

    The main difference from your algorithm is that we can convert j to i < / Code>, but only if it does not produce a fixed point, it is a while to execute (due to part of the rejection), and demand that you will be able to check whether an entry is at its original location or not , But its advantage is that it can produce all possible fission (equally, for this

    Edit:

    My algorithm is really bad: you still have the last point There is a chance not to end, and the distribution is not at all random, see the marginal distribution of simulations: marginal distribution

    An algorithm producing equally distributed excellence can be found with some reference to the problem, complete interpretation and analysis. / P>

    Seconds Edit:

    Actually known as your algorithm, and is known to produce all the chakras with the same probability. Therefore any strike that is not a cycle, but many different cycles can not be obtained with product algorithms. For example, with four elements, the conversion that exchanges between 1 and 2, and 3 and 4 is a fragmentation, but there is no cycle.

    If you do not want to get only the chakras, then Satono's algorithm is way to go, it is actually much faster than any uniform drawer algorithm because no rejection is required. .

  • Comments

    Popular posts from this blog

    mysql - BLOB/TEXT column 'value' used in key specification without a key length -

    c# - Using Vici cool Storage with monodroid -

    python - referencing a variable in another function? -