[R] Generate N random numbers with a given probability and condition

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Thu Jul 5 01:11:11 CEST 2018


On 05/07/18 10:21, Nelly Reduan wrote:

> Dear all,
> 
> I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this.
> For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested:
> 
> x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1))))
> 
> But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
> Many thanks for your time.

Your thinking requires considerable clarification.

(1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10.

(2) Hence length(seq(0,10,1)) is 11.

(3) Likewise max(seq(0,10,1)) is 10.

(4) Your prob vector is *constant* --- so specifying "prob" makes
     no difference --- the result is the same as if you omitted "prob".

(5) You need to think carefully about what you really mean by "random".
     In what way do you want the final result to be "random"?

I expect that the lecturer who assigned this problem to you  needs to 
clarify his/her thinking as well.

cheers,

Rolf Turner

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276




More information about the R-help mailing list