简体   繁体   中英

How to generate random number between 0 and 1 without 0 and 1 in R?

I want to generate random numbers using runif(n=1,min=0,max=1) in R, but I don't want the point 0 and 1, how can I modify this to achieve my goal?

I think it really generates 1 some way, see my code here

randomDim = round(runif(1)*3)+1;randomDim

  # [1] 4

The documentation for runif states:

'runif' will not generate either of the extreme values unless 'max = min' or 'max-min' is small compared to 'min', and in particular not for the default arguments.

Therefore, it is already guaranteed not to generate 0 or 1, so there is no modification needed.

By popular request: sample(1:3,1) might be the answer to your problem. It has nothing to do with runif but provides any one of the integers commonly referred to as one, two and three, with a respective probability of 1/3.

也许就是这样吗?

runif(n=1, min=1e-12, max=.9999999999)

try sample(1:9, 1000, replace = TRUE)/10

so you're randomly generating numbers from 1 to 9 and using fractions on it.

Since there is some discussion under Brent Kerby 's answer if in doubt you can always check the source code :

double runif(double a, double b)
{
    if (!R_FINITE(a) || !R_FINITE(b) || b < a)  ML_ERR_return_NAN;

    if (a == b)
    return a;
    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;
    }
}

So the algorithm "in case" rejects all values smaller or equal to zero and greater or equal to one... Exact zeros and ones are impossible. Values close to them up to floating point precision are possible as noted by Ben Bolker .

An alternative option to those listed above is to generate your numbers using a Beta distribution.

A Beta distribution with shape1=1 and shape2=1 parameters will give you a flat (ie, uniform) distribution that is mathematically limited at 0 and 1 (ie, cannot possibly take on either value).

rbeta(n, 1, 1)

also as an aside how much of the result you do not like stems from the fact that you are using the round() function?

I ask because if you were to run round(.97*3)+1 you would return a value of 4 . Similarly if you were to run round(.005*3)+1 you would get 1 . Both .97 and .005 are legal values that runif() could plausibly return. So your unwanted results may not be related to runif() returning values at its default extremes (which as has been pointed out in other answers are not included in the range of values sampled from).

In any case, if you are still unconvinced and want something instead of runif , the beta distribution with shape parameters equal to 1 will mathematically ensure that you can never get a 1 or a 0, and with that parameterization you will have an equal chance of drawing any value between 0 and 1 in a rbeta(n,1,1) distribution.

runif

head

tail

Sorry but runiif does include 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM