w********n 发帖数: 753 | 1 现在需要随机抽取5个正的integers,然后这五个数相加需要等于30.怎样在R里实现呢?
多谢各位大侠先。 |
D******n 发帖数: 2836 | 2 baozi please.
s=30
n=5
w <- sort(sample(1:(s-1),(n-1)))
draw <-c(w,s)-c(0,w)
呢?
【在 w********n 的大作中提到】 : 现在需要随机抽取5个正的integers,然后这五个数相加需要等于30.怎样在R里实现呢? : 多谢各位大侠先。
|
b*****n 发帖数: 685 | 3 这法子巧是巧,不过只能以相同概率产生随机数,我老到是做了一个function干这事,
是基于multinomial的... |
D******n 发帖数: 2836 | 4 Another limitation of this method is it only generates samples with
replacement.
【在 b*****n 的大作中提到】 : 这法子巧是巧,不过只能以相同概率产生随机数,我老到是做了一个function干这事, : 是基于multinomial的...
|
w********n 发帖数: 753 | 5 Thank you very much, BUT I forgot to mention an extra condition, all the
five generated numbers have to be greater than or equal 2. How do I do that?
PS. thanks for your response, I really appreciate it, could you show me how
to give baozi, I have never done that.
【在 D******n 的大作中提到】 : baozi please. : s=30 : n=5 : w <- sort(sample(1:(s-1),(n-1))) : draw <-c(w,s)-c(0,w) : : 呢?
|
w********n 发帖数: 753 | 6 Do you mind sharing the function?
【在 b*****n 的大作中提到】 : 这法子巧是巧,不过只能以相同概率产生随机数,我老到是做了一个function干这事, : 是基于multinomial的...
|
l*********s 发帖数: 5409 | 7 just transform the question by deducting 10 from 30 and there you go.
that?
how
【在 w********n 的大作中提到】 : Thank you very much, BUT I forgot to mention an extra condition, all the : five generated numbers have to be greater than or equal 2. How do I do that? : PS. thanks for your response, I really appreciate it, could you show me how : to give baozi, I have never done that.
|
w********n 发帖数: 753 | 8 Could you please be more specific?
【在 l*********s 的大作中提到】 : just transform the question by deducting 10 from 30 and there you go. : : that? : how
|
D******n 发帖数: 2836 | 9 s <- 30
n <- 5
lb <- 1
w <- sort(sample(1:(s-n*lb-1),(n-1)))
draw <- c(w,s-n*lb)-c(0,w)+lb
【在 w********n 的大作中提到】 : Could you please be more specific?
|
w********n 发帖数: 753 | 10 Thank you very much.
【在 D******n 的大作中提到】 : s <- 30 : n <- 5 : lb <- 1 : w <- sort(sample(1:(s-n*lb-1),(n-1))) : draw <- c(w,s-n*lb)-c(0,w)+lb
|
M****e 发帖数: 178 | 11 > x <- rep(0, 5)
> while (sum(x)!=30) {
x <- sample(2:22, size=5, replace=T)
} |
w********n 发帖数: 753 | 12 What does "!" mean in while (sum(x)!=30)?
【在 M****e 的大作中提到】 : > x <- rep(0, 5) : > while (sum(x)!=30) { : x <- sample(2:22, size=5, replace=T) : }
|
n***m 发帖数: 292 | 13 在sum(x)不等于30的情况下一直sample 直到条件满足才结束循环。不等于的意思
【在 w********n 的大作中提到】 : What does "!" mean in while (sum(x)!=30)?
|