An variant of Knuth shuffle

后端 未结 3 2015
长情又很酷
长情又很酷 2021-01-19 09:54

This is a very hard but interesting probability question related to Knuth shuffle.

When looping for each element, the swap is performed for the current element with

3条回答
  •  长情又很酷
    2021-01-19 10:27

    In a proper Knuth shuffle, you loop over i elements choosing one of 52 to swap with element 1, then one of 51 remaining to swap with element 2, one of of 50 remaining to swap with element 3, etc. Once an element has been placed into the growing set 1,2,3... it will never move again. So the odds of element i ending up in position 1 is exactly 1/52--it can only happen on the first loop. The odds of it ending up in cell 2 are the odds of it not being in cell 1 (51/52) times the odds of it being chosen on the second pass (1/51). The 51s cancel, leaving 1/52. Likewise, the chance of element i ending up in position 3 is 51/52 * 50/51 * 1/50, again 1/52. So every element has an equal chance to end up in each cell.

    In your shuffle, already-placed elements may be chosen again for later switching, which makes calculating the odds very difficult.

提交回复
热议问题