generate random integers between two values with a given probability using R

前端 未结 2 751
不思量自难忘°
不思量自难忘° 2020-12-16 04:56

I have the following four number sets:

A=[1,207];
B=[208,386];
C=[387,486];
D=[487,586].

I need to generate 20000 random numbers between 1

2条回答
  •  执念已碎
    2020-12-16 05:24

    You can directly use sample, more specifcally the probs argument. Just divide the probability over all the 586 numbers. Category A get's 0.5/207 weight each, etc.

    A <- 1:207
    B <- 208:386
    C <- 387:486
    D <- 487:586
    L <- sapply(list(A, B, C, D), length)
    
    x <- sample(c(A, B, C, D),
                size = 20000,
                prob = rep(c(1/2, 1/6, 1/6, 1/6) / L, L),
                replace = TRUE)
    

提交回复
热议问题