简体   繁体   English

从ArrayList抓取随机对象不是随机的

[英]Grabbing random object from ArrayList is not random

I am creating a method where if you pass in a parameter of type Random, then it will return a random object. 我正在创建一个方法,如果您传入类型为Random的参数,则它将返回一个随机对象。 Here is basically what I am trying to do: 这基本上是我想要做的:

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

The random list has this the following strings: [2, 2, 2, 1, 1, 1, 1, c, c, c, a, a, a, a] 随机列表具有以下字符串: [2, 2, 2, 1, 1, 1, 1, c, c, c, a, a, a, a] 2,2,2,1,1,1,1,1 [2, 2, 2, 1, 1, 1, 1, c, c, c, a, a, a, a]

Then I made a driver with the following code 然后我用以下代码制作了一个驱动程序

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

However my outputs are not coming out random. 但是我的输出不是随机的。 I used the debugger and found out that my choose method generates an integer that is relatively low, so it will always print out either 2's or 1's but never c's or a's. 我使用了调试器,发现我的select方法生成的整数相对较低,因此它将始终打印出2或1,而不会打印c或a。 I can't figure out why this is happening and help would be greatly appreciated. 我不知道为什么会这样,将不胜感激。

EDIT: Problem was solved. 编辑:问题已解决。 I left out alot of detail, but when I called the size() method, that was something I overwrote which had an error which would return a smaller number than I would have liked. 我省略了很多细节,但是当我调用size()方法时,我改写了一些东西,该东西有一个错误,该错误返回的数字比我想要的小。 Thanks to dtech for noticing my silly mistake. 感谢dtech注意到我的愚蠢错误。 Thank you for everyone who tried to help me! 谢谢所有试图帮助我的人!

At first glance nothing seems wrong with the code, so it might just be a random result. 乍看之下,代码似乎没有问题,因此可能只是随机结果。 But your "Print it and check" method is very unreliable. 但是您的“打印并检查”方法非常不可靠。 Just use something like this: 只需使用以下内容:

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()));
}

This will print on average something like: 2: 1429 1: 2857 c: 2143 a: 2857 平均打印如下:2:1429 1:2857 c:2143 a:2857

Only if the numbers differ creatly you should be concerned. 仅当数字确实不同时,您才应关注。

Also make sure that you use the new Random() constructor, not new Random(somenumber). 还要确保使用新的Random()构造函数,而不是新的Random(somenumber)。 If you use the latter you will get the same number sequence every time. 如果使用后者,则每次都会获得相同的数字序列。

send you random initialization code, are you getting exactly the same results each time? 发送给您随机初始化代码,您每次得到的结果完全一样吗? are you using a seed to create the Random object? 您是否正在使用种子创建随机对象?

Here is the code I used. 这是我使用的代码。 You need to provide more code to see why yours doesn't work. 您需要提供更多代码,以查看您的代码为什么不起作用。

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
           }

    }
}

prints 版画

1ca1caa1a2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM