samedi 17 septembre 2016

Java Random Utility Generating Too Many 0's And Static Numbers

The line "birthdays[j] = rnd.nextInt(365);" seems to generate extra 0's in the int[] birthdays array. It also seems to add an EXTRA 0 into the array and generate static values depending on how many simulations I run and how many birthdays I generate. For instance, if I do 5 simulations and enter a 3 for the number of people in each simulation's "birthday pool" I always get an array of [0, 0, 289, 362].

Any help understanding the problem would be greatly appreciated.

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




Aucun commentaire:

Enregistrer un commentaire