【问题标题】:Grabbing random object from ArrayList is not random从 ArrayList 抓取随机对象不是随机的
【发布时间】:2011-03-06 16:07:04
【问题描述】:

我正在创建一个方法,如果你传入一个随机类型的参数,那么它将返回一个随机对象。这基本上是我想要做的:

public T choose(Random r) {
    int randomInt = r.nextInt(randomList.size()); // randomList is just a instance variable
    return randomList.get(randomInt);   
}

随机列表包含以下字符串:[2, 2, 2, 1, 1, 1, 1, c, c, c, a, a, a, a]

然后我用下面的代码做了一个驱动

 for (int i = 0; i < 10; i++) {
        System.out.print(rndList.choose(rnd)); // rnd is initialized as a static Random variable
    }

但是我的输出并不是随机的。我使用了调试器,发现我的选择方法会生成一个相对较低的整数,因此它总是会打印出 2 或 1,但不会打印出 c 或 a。我不知道为什么会发生这种情况,我们将不胜感激。

编辑:问题已解决。我遗漏了很多细节,但是当我调用 size() 方法时,我重写了它,它有一个错误,返回的数字比我想要的要小。感谢 dtech 注意到我的愚蠢错误。感谢所有试图帮助我的人!

【问题讨论】:

  • 你试过运行它50次吗?
  • 另外,您是如何构建随机数生成器的?使用Random() 还是Random(someConstantIntegerValue)?如果您使用后者,请注意每次运行程序时都会得到相同的“随机”序列。
  • 你能创建一个完整的可运行的测试程序吗?您的错误似乎超出了您在此处发布的代码。
  • 当我运行这段代码时,我得到了输出 a1c211a121 你得到了什么,你期望得到什么?
  • 第一个代码示例中的列表名为“randomList”,第二个代码示例中的列表名为“rndList”。只是一个错字?

标签: java list random arraylist


【解决方案1】:

乍一看,代码似乎没有任何问题,因此它可能只是一个随机结果。但是您的“打印并检查”方法非常不可靠。只需使用这样的东西:

final int N = 10000; // test 10.000 times
HashTable<Object, Integer> count = new HashTable(N);
for(int i=0;i < N;i++){
    Object o = rndList.choose(rnd);
    count.put(o, (count.get(o)==null?0:count.get(o))+1);
}
for(Map.Entry<Object, Integer> map : count.entrySet()){
    System.out.println(String.format("%s: %d", map.getKey().toString(), map.getValue()));
}

这将平均打印如下内容: 2:1429 1:2857 c: 2143 答:2857

只有当数字明显不同时,你才应该担心。

还要确保使用新的 Random() 构造函数,而不是新的 Random(somenumber)。如果你使用后者,你每次都会得到相同的数字序列。

【讨论】:

  • 我把你的方法放在我的驱动程序中,我一直得到 2 大约 7500 和 1 大约 2500,没有提到 a 或 c。我不知道发生了什么。在我的驱动程序中,我创建了一个静态变量 Random,如下所示: static Random rnd = new Random();我不知道发生了什么!
  • 那不是你的 randomList.size() 的错误吗?这些数字似乎来自返回 4 的 randomList.size(),这是列表中的元素数。您不是使用返回 Set/List 中唯一元素数量的方法吗?您不是使用 Set 类而不是 List 类吗?
  • 哇,你解决了我的问题哈哈。我知道我在代码中遗漏了很多东西,但我正在编写地图的抽象,我使用的地图大小实际上比 ArrayList 小。
【解决方案2】:

给你发送随机初始化代码,你每次得到完全相同的结果吗?您是否使用种子来创建 Random 对象?

【讨论】:

    【解决方案3】:

    这是我使用的代码。您需要提供更多代码来查看为什么您的代码不起作用。

    public class Main<T> {
        private List<T> randomList = new ArrayList<T>();
    
        public  T choose(Random r) {
            int randomInt = r.nextInt(randomList.size()); // randomList is just a instance variable
            return randomList.get(randomInt);
        }
    
    
        public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
            Main<String> rndList = new Main<String>();
            rndList.randomList.addAll(Arrays.asList("2, 2, 2, 1, 1, 1, 1, c, c, c, a, a, a, a".split(", ")));
    
            Random rnd = new Random();
            for (int i = 0; i < 10; i++) {
                   System.out.print(rndList.choose(rnd)); // rnd is initialized as a static Random variable
               }
    
        }
    }
    

    打印

    1ca1caa1a2
    

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 2021-10-23
      • 2017-08-13
      • 2017-11-11
      • 2012-10-04
      • 1970-01-01
      • 2012-11-03
      • 2011-11-07
      • 1970-01-01
      相关资源
      最近更新 更多