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

时间:2016-09-18 01:49:47

标签: 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;
    }
}   

3 个答案:

答案 0 :(得分:3)

  

行“birthdays [j] = rnd.nextInt(365);”似乎在int [] birthdays数组中生成了额外的0。

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

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

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

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

考虑一下......

答案 1 :(得分:1)

除非明确指定,否则

int数组默认初始化为0。有关阵列的信息,请参阅此Oracle tutorial

答案 2 :(得分:-2)

我自己发现了这个问题。问题是拥有“Arrays.sort(生日);”循环内的语句。这产生了额外的0。