gpt4 book ai didi

r - 选择一个随机数,其值总是比最后选择的随机数增加

转载 作者:行者123 更新时间:2023-12-04 09:06:55 24 4
gpt4 key购买 nike

我将如何有效地对值1:n进行1-by-1升序随机抽样,确保每个随机抽样的值始终大于
以前的值?

例如。:

For the values 1:100, get a random number, say which is 61. (current list=61)
Then pick another number between 62 and 100, say which is 90 (current list=61,90)
Then pick another number between 91 and 100, say which is 100.
Stop the process as the max value has been hit (final list=61,90,100)



我一直呆在圈地里,以这种笨拙的方式思考:
a1 <- sample(1:100,1)

if(a1 < 100) {
a2 <- sample((a+1):100,1)
}

etc etc...

我想报告一个最终向量是a1,a2,a(n)的串联:
result <- c(a1,a2)

即使这听起来像是一个作业问题,但事实并非如此。值得庆幸的是,多年前,我离开了家庭作业的日子。

最佳答案

来晚了,但是我认为这会震撼你的世界:

unique(cummax(sample.int(100)))

关于r - 选择一个随机数,其值总是比最后选择的随机数增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12594472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com