Вы можете просто использовать индексы, взятые из 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);