Java Collections.shuffle()怪异的行为 [英] Java Collections.shuffle() weird behaviour

查看:334
本文介绍了Java Collections.shuffle()怪异的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了奇怪的事情.

我有一个很大的长数字ArrayList.它包含约20万个按升序排列的数字.这些数字总是不同的.它们不一定是连续的,但其中的某些组通常是连续的.

I have a big ArrayList of Long numbers. It contains about 200k numbers in ascending order. These numbers are always distinct; they are not necessarily consecutive, but some groups of them usually are.

我想从此列表中提取5k的排序样本,所以基本上这是我的方法:

I want to extract a sorted sample of 5k from this list, so basically this is my approach:

  • 我打java.util.Collections.shuffle(list);
  • 我从改组后的list
  • 中提取前5k个元素
  • 我按升序对提取的元素进行排序
  • I call java.util.Collections.shuffle(list);
  • I extract the first 5k elements from the now shuffled list
  • I sort the extracted elements in ascending order

但是,我的结果有些奇怪. 我提取的许多随机Long似乎彼此接近,即使不是连续的.例如,我得到了:

My result is somewhat weird, though. Many of my extracted random Longs seem suspiciously close to each other, if not even consecutive. For instance, I got:

...
38414931,
38414932,
38414935,
38414937,
38414938,
38414939,
38414941,
...

这不一定看起来是随机的:/

This does not definitely look random :/

还有一件更奇怪的事情. 在进行调试时,我尝试将初始list和提取的样本都写入文件以进行比较. 如果这样做,我的问题似乎消失了,提取的Longs看起来像是正确的随机数.

There is an even stranger thing. While debugging this, I tried to write into files both the initial list and the extracted sample in order to compare them. If I do this, my problem seems to disappear, and my extracted Longs look like proper random numbers.

当然,我已经重复了很多次,每次我都经历了这两种行为.

I have repeated this many times, of course, and every time I did I experienced these two behaviours.

我错过了什么吗?

这是我正在使用的代码:

Here is the code I am using:

List<Long> allNumbers = <getting my list>;

--->如果在这里我将allNumbers写入文件,则似乎工作正常

---> if here I write allNumbers into a file, it seems to work fine

Collections.shuffle(allNumbers);
HashSet<Long> randomNumbers = new HashSet<>();
for (int i = 0; i < 5000; i++) {
   randomNumbers.add(allNumbers.get(i));
}

推荐答案

以下是最小,完整和可验证的示例您将按预期输出一些随机的,递增的数字.请注意,除了输入部分之外,我的代码与您的代码相同.因此,或者您的问题出在尚未显示的代码中,或者即使存在连续的数字序列(即使是随机分布的序列),输出也可以.

Here is a Minimal, Complete, and Verifiable example for you, that outputs some random, increasing numbers, as you expect. Note that my code is the same as yours, except for the input part. So either your problem is in the code you haven't shown yet or the output is fine even if there are sequences of consecutive numbers, which you would expect even with a random distribution.

public static void main(String[] args) {
  List<Long> allNumbers = new ArrayList<>();
  for (long i = 0; i < 2_000; i++) allNumbers.add(i);

  Collections.shuffle(allNumbers);
  Set<Long> randomNumbers = new HashSet<>();

  for (int i = 0; i < 50; i++) randomNumbers.add(allNumbers.get(i));

  randomNumbers.stream().sorted().forEach(n -> System.out.print(n + " "));
}

示例输出:

30 149 233 255 301 357 361 391 412 413 423 480 481 ...

30 149 233 255 301 357 361 391 412 413 423 480 481 ...

这篇关于Java Collections.shuffle()怪异的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆