如何证明 matlab 中的 randperm() 是公平的

How to show that randperm() in matlab is fair

假设,我想(凭经验)证明来自 matlab 的 randperm(n,k) 确实从 n 个元素的集合 N 中生成大小为 k 的均匀分布随机样本。在重复绘制后,如何绘制出现次数除以从 N 中提取的 k 个子集的总数?

您可以简单地使用从 randperm 中提取的索引来增加计数器向量。

n=1e5;
k=1e4;
maxiter = 1e5;

% This array will be used to count the number of times each integer has been drawn
count=zeros(n,1);

for ii=1:maxiter
    p=randperm(n,k);
    % p is a vector of k distinct integers in the 1:n range
    % the array count will be incremented at indices given by p
    count(p)=count(p)+1;
end

% A total of k*maxiter integers has been drawn and they should be evenly
% distributed over n values
% The following vector should have values close to 1 for large values of maxiter
prob = count*n/(k*maxiter);