Mercurial > octave
diff scripts/general/randi.m @ 26617:98afb8bbd1f6 stable
maint: Strip trailing whitespace from source files.
* NEWS, GLCanvas.cc, dialog.cc, error.cc, ft-text-renderer.cc,
sysdep.cc, utils.cc, pt-jit.cc, oct-rl-hist.c, makeUniqueStrings.m,
randi.m, waitbar.m, clearvars.m, sqp.m, fplot.m, openfig.m,
__print_parse_opts__.m, octave-svgconvert.cc: Strip trailing whitespace.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 24 Jan 2019 19:18:51 +0000 |
parents | 00f796120a6d |
children | b442ec6dda5c |
line wrap: on
line diff
--- a/scripts/general/randi.m Thu Jan 24 19:06:40 2019 +0000 +++ b/scripts/general/randi.m Thu Jan 24 19:18:51 2019 +0000 @@ -102,20 +102,20 @@ elseif (nargin == 2 && isscalar (varargin{1})) varargin(2) = varargin(1); endif - + ## Rejection Algorithm to guarantee unbiased results. See bug #54619. rng = (imax - imin) + 1; # requested range N = prod ([varargin{:}]); # number of requested elements K = floor ((flintmax () + 1) / rng); # number of primary integers ... # mapped to single output p = (K*rng) / (flintmax () + 1); # expected proportion of used primaries - - do + + do M = ceil (N/p + 10*sqrt (N/p - N)); # number of requested primary integers r_prim = floor (rand (M,1) * (flintmax () + 1)); r_prim = r_prim(r_prim < K*rng); until (numel (r_prim) >= N) # should practically always be true - + ri = imin + floor (reshape (r_prim(1:N), varargin{:}) / K); if (! strcmp (rclass, "double"))