Skip to content

Commit

Permalink
make sure even user-supplied RNGs have 0 < X < 1
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@39398 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
ripley committed Sep 19, 2006
1 parent bdacf7f commit 9df0e77
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/library/stats/man/Uniform.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ runif(n, min=0, max=1)
\eqn{X \equiv u}{X == u} is assumed, although there is no density in
that case and \code{dunif} will return \code{NaN} (the error condition).

\code{runif} will not generate either of the extreme values (unless
\code{max = min}.
\code{runif} will not generate either of the extreme values unless
\code{max = min} or \code{max-min} is small compared to \code{min},
and in particular not for the default arguments.
}
\references{
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
Expand Down
9 changes: 7 additions & 2 deletions src/nmath/runif.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ double runif(double a, double b)

if (a == b)
return a;
else
return a + (b - a) * unif_rand();
else {
double u;
/* This is true of all builtin generators, but protect against
user-supplied ones */
do {u = unif_rand();} while (u <= 0 || u >= 1);
return a + (b - a) * u;
}
}

0 comments on commit 9df0e77

Please sign in to comment.