Last active
August 29, 2015 14:00
-
-
Save texzhichen/11383736 to your computer and use it in GitHub Desktop.
Compare the KFY shuffle with naive shuffle by a=[1,2,3]. There are 6 combinations. The experiment result shows that KFY shuffle is unbiased and the number of distinct combination closes to round(max_iter/6). But naive shuffle is biased. Refer to http://blog.codinghorror.com/the-danger-of-naivete/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% KFY shuffle | |
n = 3; | |
max_iter = 1e5; | |
count = zeros(17,1); | |
for iter = 1:max_iter | |
a = 1:n; | |
for i = n:-1:2 | |
j = randi(i,1,1); | |
t = a(i); | |
a(i) = a(j); | |
a(j) = t; | |
end | |
key = a(1)*4+a(2)*2+a(3)*1; | |
count(key) = count(key)+1; | |
end | |
idx = [11,12,13,15,16,17]; | |
disp(count(idx)); | |
figure; barh(count(idx)); | |
%% naive shuffle | |
n = 3; | |
max_iter = 1e5; | |
count = zeros(17,1); | |
for iter = 1:max_iter | |
a = 1:n; | |
for i = 1:n | |
j = randi(n,1,1); | |
t = a(i); | |
a(i) = a(j); | |
a(j) = t; | |
end | |
key = a(1)*4+a(2)*2+a(3)*1; | |
count(key) = count(key)+1; | |
end | |
idx = [11,12,13,15,16,17]; | |
disp(count(idx)); | |
figure; barh(count(idx)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment