获取种子来生成一组特定的伪休息号

发布于 2025-02-10 13:28:40 字数 184 浏览 2 评论 0原文

我想知道R中是否有一种方法可以找到生成一组特定数字的特定种子。

例如:

sample(1:300, 10)

我想找到给我的种子,作为先前代码的输出:

58 235 243  42 281 137   2 219 284 184

I was wondering if there is a way in R to find the specific seed that generates a specific set of numbers;

For example:

sample(1:300, 10)

I want to find the seed that gives me, as the output of the previous code:

58 235 243  42 281 137   2 219 284 184

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2025-02-17 13:28:40

据我所

desired_output <- c(58, 235, 243, 42, 281, 137, 2, 219, 284, 184)
MAX_SEED <- .Machine$integer.max
MIN_SEED <- MAX_SEED * -1
i  <- MIN_SEED
while (i < MAX_SEED - 1) {
    set.seed(i)
    actual_output <- sample(1:300, 10)
    if (identical(actual_output, desired_output)) {
        message("Seed found! Seed is: ", i)
        break
    }
    i  <- i + 1
}

知道跑步大约需要7个小时。另外,这正是您可以在单独的线程中并行运行的任务,以将时间缩短了很多。

编辑:更新以包括我未考虑的负种子。因此,实际上可能需要两倍的时间。

As far as I know there is no elegant way to do this, but you could brute force it:

desired_output <- c(58, 235, 243, 42, 281, 137, 2, 219, 284, 184)
MAX_SEED <- .Machine$integer.max
MIN_SEED <- MAX_SEED * -1
i  <- MIN_SEED
while (i < MAX_SEED - 1) {
    set.seed(i)
    actual_output <- sample(1:300, 10)
    if (identical(actual_output, desired_output)) {
        message("Seed found! Seed is: ", i)
        break
    }
    i  <- i + 1
}

This takes 11.5 seconds to run with the first 1e6 seeds on my laptop - so if you're unlucky then it would take about 7 hours to run. Also, this is exactly the kind of task you could run in parallel in separate threads to cut the time down quite a bit.

EDIT: Updated to include negative seeds which I had not considered. So in fact it could take twice as long.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文