作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个包含 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/
我是一名优秀的程序员,十分优秀!