Drawing a random number

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Drawing a random number

Bogaso
Hi,

I would like to draw an Integer from a range of [10, 1000] inclusive,
however that random integer should be outside of a pre-defined vector of
integers.

Let say I draw an integer as below

as.integer(runif(1, 10, 1000))

and my pre-defined vector is

Vec = c(563, 453, 897, 567)

The policy is my drawn random integer should never be equal to any item
from Vec

Ofcourse I can use ifelse() to achieve the same, however I was wondering if
there is any direct way to get the same.

Thanks,

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing a random number

R help mailing list-2
sample( setdiff(10:1000, Vec), size=1)

Also, note that as.integer(runif(1, min, max)) will almost never return max.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Thu, Nov 29, 2018 at 11:14 AM Christofer Bogaso <
[hidden email]> wrote:

> Hi,
>
> I would like to draw an Integer from a range of [10, 1000] inclusive,
> however that random integer should be outside of a pre-defined vector of
> integers.
>
> Let say I draw an integer as below
>
> as.integer(runif(1, 10, 1000))
>
> and my pre-defined vector is
>
> Vec = c(563, 453, 897, 567)
>
> The policy is my drawn random integer should never be equal to any item
> from Vec
>
> Ofcourse I can use ifelse() to achieve the same, however I was wondering if
> there is any direct way to get the same.
>
> Thanks,
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.