Question
This is to write a simple set of Java code to accomplish the following: Please ignore the previous question that is similar to this one
This is to write a simple set of Java code to accomplish the following:
Please ignore the previous question that is similar to this one - that one includes typos - this question is the correct one!
The derangement problem can be phrased as follows. There are n people, and each of these people have a coat. They go to a bar and check in their coats. They go, socialize and come back to pick up their coats but it is hard for them to recognize their, so they just pick up a random coat.
Write java code to find the following:
1. What is the probability of 0 people getting their coats?
2. What is the average number (expected value) of people who get their coats back?
I personally know that the answer comes out to 1/e as the number of people gets larger according to probability theory, but this question asks for java code to run.
We are provided with a class called RandomOrderGenerator.RandomOrderGenerator, which has exactly one method called getRandomOrder that returns a random ordering (permutation) of numbers from 1 to n.
To get a random ordering of the numbers from 1 to 10, we do the following: int[] randomArrangementFrom1To10 = RandomOrderGenerator.getRandomOrder(10)).
Now in order to do the simulation we will be using the following idea. We assume that the people are numbered from 1 to n and the coats are also correspondingly numbered from 1 to n. An arrangement of the numbers from 1 to n then corresponds to one particular result of the random experiment of n people picking up n coats in a random manner. For instance, if we have 10 people and the random arrangement we have is [1, 6, 5, 8, 3, 4, 7, 10, 9, 2] then this means that person 1 got coat 1, person 2 got coat 6, and so on. As you can see in this particular scenario, it means that 3 people got their coat back and the rest got someone elses coat back.
We have to write one more class in the same project called CoatExperimentSimulator. It has one single instance variable called numberOfPeople, and it has a very simple constructor: CoatExperimentSimulator(int numPpl) { numberOfPeople = numPpl; }
With the above in mind, we have to create 4 methods:
1. public int numPplWhoGotTheirCoat (int[] permutation) - which determines the number of people who got their coat back given an array that represents a random order.
2. public int[] simulateCoatExperiment(int iterations) - which simulates the coat problem multiple times and returns an array that contains the number of people who got their coats back in each experiment.
3. public double answerToQuestionA(int[] results) given the results of n iterations of the coats experiment, this computes the probability of 0 people getting their coats back by taking the number of times 0 people got their coats back in these n iterations and dividing that number by n.
4. public double answerToQuestionB(int[] results) given the results of n iterations of the coats experiment, this computes the average number of people who get their coats back.
At the very end, the code should print 3 things:
1. System.println(probability); - the probability of 0 people getting their coats back
2. System.println(average); - the average number of people who got their coats back
3. System.out.println(estimate); - the estimate of the value of e that you got from this procedure
This may be a lot but partial help or any java code to accomplish the above would be appreciated!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started