user4592991
user4592991

Reputation:

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

Upvotes: 5

Views: 39002

Answers (7)

Jones
Jones

Reputation: 1

The issue is in your round() function. You are missing an argument.

round(runif(1)*3, X)+1

Where X is the number of digits you want to use.

Upvotes: 0

Matt Barstead
Matt Barstead

Reputation: 235

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 (i.e., uniform) distribution that is mathematically limited at 0 and 1 (i.e., 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.

Upvotes: 1

sweetmusicality
sweetmusicality

Reputation: 937

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

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

Upvotes: 2

Tim
Tim

Reputation: 7464

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.

Upvotes: 0

Pewi
Pewi

Reputation: 1134

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.

Upvotes: 2

Dominic Comtois
Dominic Comtois

Reputation: 10401

Maybe something like this, simply?

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

Upvotes: 4

Brent Kerby
Brent Kerby

Reputation: 1447

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.

Upvotes: 20

Related Questions