- Joined
- Sep 20, 2022
- Messages
- 338
- Reaction score
- 42
I was reviewing a sudoku program, which lead me to consider this pseudocode for shuffling 9 elements.
At first glance it seems fair, but there's bias here.
Let P(s,d) be the probability that an object that starts in position s (source) finishes in position d (destination).
Very easy to estimate with a monte carlo simulation, but I was curious about the exact value.
I tried a pen & paper mathematical approach, but one of the cases that can happen to an object is very tricky to express.
In the end I got an exact answer using an approach similar to counting paths down Pascal's triangle.
In the final results there was a wide range in values and some unexpected symmetry.
Code:
FOR i = 1 TO 9
r = random, from 1 to 9
swap element i and r
Let P(s,d) be the probability that an object that starts in position s (source) finishes in position d (destination).
Very easy to estimate with a monte carlo simulation, but I was curious about the exact value.
I tried a pen & paper mathematical approach, but one of the cases that can happen to an object is very tricky to express.
In the end I got an exact answer using an approach similar to counting paths down Pascal's triangle.
In the final results there was a wide range in values and some unexpected symmetry.
Code:
P(3,2) = P(8,7)
P(a,9-b) = P(b+1,10-a)