gpt4 book ai didi

java - 如何从java数组中获取非随机样本

转载 作者:行者123 更新时间:2023-11-30 03:30:38 25 4
gpt4 key购买 nike

假设我有一个包含 200 万个 id 的数组。我现在想要检索这些 id 的样本。目前,我使用此问题答案 here 中建议的随机抽样。 。

private static void shuffleScoreArray(ScoreDoc[] ar) {
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
ScoreDoc a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}

这非常有效,但是我现在如何检索非随机(或多或少分布良好 - 不必 100% 均等)采样?在这种情况下,非随机意味着如果我使用相同的输入数组调用该函数两次,我两次都会得到相同的结果样本。

我刚刚对 SO 和 Google 做了很多研究,但在这种情况下找不到帮助我的方法。大多数 SO 方法似乎都涉及随机采样方法或增加性能步骤。

我可以想象(但不知道是否有效)是你总是使用相同的 Random 对象,但我不确定如何将其按照预期的 java 代码工作。

非常感谢您与我分享的每一个想法和答案。

最佳答案

将种子传递给 RNG。而不是这个:

Random rnd = new Random();

Use this :

Random rnd = new Random(12345l);

对于相同的种子值,来自 RNG 的值将是相同的。

关于java - 如何从java数组中获取非随机样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140904/

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