Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double numKids = 0;
- for(int i = 0 ; i <numTrials ; i++) {
- //System.out.println("New Couple");
- boolean boy = false;
- boolean girl = false;
- while(boy==false || girl==false) {
- double gender = Math.random();
- if (gender < .5) {
- //System.out.println("Boy");
- numKids= numKids+1;
- boy=true;
- }
- else {
- //System.out.println("Girl");
- numKids= numKids+1;
- girl=true;
- }
- }
- }double average= numKids/numTrials;
- return average;
- }
- /*
- * This time, compute the average number of kids a couple needs to have
- * until they have a "team" of all girls or all boys of the given size.
- *
- * For example, if I am looking for a team size of 4, and I have:
- * girl, boy, boy, boy, girl, boy
- * Then it took 6 kids before I have a team of 4 of the same gender.
- *
- * You can probably copy and paste a large amount of code from the previous problem.
- */
- public double avgKidsToTeam(int size, int numTrials){
- double numKids = 0;
- for(int i = 0 ; i <numTrials ; i++) {
- //System.out.println("New Couple");
- double numboy = 0;
- double numgirl = 0;
- while(numboy <size || numgirl <size) {
- double gender = Math.random();
- if (gender < .5) {
- //System.out.println("Boy");
- numKids= numKids+1;
- numboy= numboy +1;
- }
- else {
- //System.out.println("Girl");
- numKids= numKids+1;
- numgirl= numgirl +1;
- }
- }
- }double average= size/numKids;
- return average;
- }
- public static void main(String[] args){
- Statistics stats = new Statistics();
- // The largest answer might be anything, but the average should be about 3.
- System.out.println(stats.avgKidsToBoth(1000000));
- // Should be about 2.5
- System.out.println(stats.avgKidsToTeam(2,1000000));
- // Should be about 4.12
- System.out.println(stats.avgKidsToTeam(3,1000000));
- // Should be about 5.81
- System.out.println(stats.avgKidsToTeam(4,1000000));
- // Should be about 7.53
- System.out.println(stats.avgKidsToTeam(5,1000000));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement