% This isn't thorough statistical checking, just some quick sanity checking to % catch obvious blunders: K = 5; probs = rand(K, 1); probs = probs/sum(probs); num_samples = 1e6; samples = discreternd(num_samples, probs); if ~exist('histc') histc = @my_histc; end tic counts = histc(samples, 1:K); toc expected = num_samples*probs; error_bar_ish = sqrt(probs.*(1-probs)*num_samples); standard_devs_out = abs(counts-expected)./error_bar_ish testsmall(standard_devs_out, 3); % As a side note, decided to compute counts in a round-about way: tic [dummy, counts2] = unique_totals([samples;(1:K)'], ones(num_samples + K, 1)); counts2 = counts2 - 1; toc % Bad way of doing it! (Unless histc implementation is terrible, like my % initial quick hack replacement for octave). testequal(counts, counts2); % Hope zero probability events never occur: probs(3) = 0; samples = discreternd(1e6, probs); testequal(sum(samples==3), 0); test_exit