Java 随机实用程序生成过多的 0 和静态数字

标签 java arrays random zero

birthdays[j] = rnd.nextInt(365);似乎在int[]birthdays数组中生成了额外的0。它还似乎在数组中添加了一个 EXTRA 0 并根据我运行的模拟次数和生成的生日次数生成静态值。例如,如果我进行 5 次模拟,并为每次模拟的“生日池”中的人数输入 3,我总是会得到一个 [0, 0, 289, 362] 数组。

任何帮助理解该问题的帮助将不胜感激。

public static void main(String[] args) {
        System.out.println("Welcome to the birthday problem Simulator\n");
        String userAnswer="";
        Scanner stdIn = new Scanner(System.in);
        do {
            int [] userInput = promptAndRead(stdIn);
            double probability = compute(userInput[0], userInput[1]);

            // Print results
            System.out.println("For a group of " + userInput[1] + " people, the probability");
            System.out.print("that two people have the same birthday is\n");
            System.out.println(probability);

            System.out.print("\nDo you want to run another set of simulations(y/n)? :");

            //eat or skip empty line
            stdIn.nextLine();
            userAnswer = stdIn.nextLine();


        } while (userAnswer.equals("y"));

        System.out.println("Goodbye!");
        stdIn.close();
    }

    // Prompt user to provide the number of simulations and number of people and return them as an array
    public static int[] promptAndRead(Scanner stdIn) {

        int numberOfSimulations = 0;
        while(numberOfSimulations < 1 || numberOfSimulations > 50000) {
            System.out.println("Please Enter the number of simulations to do. (1 - 50000) ");
            numberOfSimulations = stdIn.nextInt();
        }

        int sizeOfGroup = 0;
        while(sizeOfGroup < 2 || sizeOfGroup > 365) {
            System.out.println("Please Enter the size of the group of people. (2 - 365) ");
            sizeOfGroup = stdIn.nextInt();
        }

        int[] simulationVariables = {numberOfSimulations, sizeOfGroup};
        return simulationVariables;
    }


    // This is the method that actually does the calculations.
    public static double compute(int numOfSims, int numOfPeeps) {

        double numberOfSims = 0.0;
        double simsWithCollisions = 0.0;
        int matchingBirthdays = 0;
        int[] birthdays = new int[numOfPeeps + 1];
        int randomSeed = 0;

        for(int i = 0; i < numOfSims; i++)
        {   
            randomSeed++;
            Random rnd = new Random(randomSeed);
            birthdays = new int[numOfPeeps + 1];
            matchingBirthdays = 0;

            for(int j = 0; j < numOfPeeps; j++) {
                birthdays[j] = rnd.nextInt(365);
                Arrays.sort(birthdays);
            }

            for(int k = 0; k < numOfPeeps; k++) {
                if(birthdays[k] == birthdays[k+1]) {
                    matchingBirthdays++;
                }
            }

            if(matchingBirthdays > 0) {
                simsWithCollisions = simsWithCollisions + 1;
            }
        }
        numberOfSims = numOfSims;
        double chance = (simsWithCollisions / numberOfSims);
        return chance;
    }
}   

最佳答案

The line "birthdays[j] = rnd.nextInt(365);" seems to generate extra 0's in the int[] birthdays array.

嗯,事实并非如此。数组元素从零开始。

该语句实际上的作用是生成一个随机数(从 0 到 364)并将其分配给数组的一个元素;即第 j 个元素。这不是您的问题所需要的。

现在,我们可以为您修复您的代码,但这违背了您家庭作业的目的。相反,我会给你一个提示:

  • birthdays 数组应该包含一年中每一天生日的人数 COUNT。你必须数一下它们。一次一个。

想想看...

关于Java 随机实用程序生成过多的 0 和静态数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39553326/

相关文章:

java - JUnit 如何测试 "printlist"?

python - 使用嵌套结构按列连接 NumPy 数组

c - 使用后递增运算符的数组地址有什么问题?

python - Pandas random_state 究竟做了什么?

c 时间上的随机数

javascript - 使用javascript循环生成1000个随机10位数字

java - 无法将 JAR 文件加载为 JBoss 7.1 中的资源

java - 使用从 Spring Controller 获取的值填充表单

java - 当我访问某个网址时,如何下载在网络浏览器中自动下载的文件?

javascript - 如何比较 2 个数组并在没有时添加键