Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1.  
  2.  
  3. double numKids = 0;
  4.  
  5.  
  6. for(int i = 0 ; i <numTrials ; i++) {
  7. //System.out.println("New Couple");
  8. boolean boy = false;
  9. boolean girl = false;
  10. while(boy==false || girl==false) {
  11. double gender = Math.random();
  12. if (gender < .5) {
  13. //System.out.println("Boy");
  14. numKids= numKids+1;
  15.  
  16. boy=true;
  17. }
  18.  
  19. else {
  20. //System.out.println("Girl");
  21. numKids= numKids+1;
  22.  
  23. girl=true;
  24. }
  25. }
  26. }double average= numKids/numTrials;
  27. return average;
  28.  
  29. }
  30.  
  31. /*
  32. * This time, compute the average number of kids a couple needs to have
  33. * until they have a "team" of all girls or all boys of the given size.
  34. *
  35. * For example, if I am looking for a team size of 4, and I have:
  36. * girl, boy, boy, boy, girl, boy
  37. * Then it took 6 kids before I have a team of 4 of the same gender.
  38. *
  39. * You can probably copy and paste a large amount of code from the previous problem.
  40. */
  41. public double avgKidsToTeam(int size, int numTrials){
  42.  
  43. double numKids = 0;
  44.  
  45.  
  46. for(int i = 0 ; i <numTrials ; i++) {
  47. //System.out.println("New Couple");
  48. double numboy = 0;
  49. double numgirl = 0;
  50. while(numboy <size || numgirl <size) {
  51.  
  52. double gender = Math.random();
  53. if (gender < .5) {
  54. //System.out.println("Boy");
  55. numKids= numKids+1;
  56. numboy= numboy +1;
  57.  
  58. }
  59.  
  60. else {
  61. //System.out.println("Girl");
  62. numKids= numKids+1;
  63. numgirl= numgirl +1;
  64.  
  65. }
  66. }
  67. }double average= size/numKids;
  68. return average;
  69.  
  70. }
  71.  
  72. public static void main(String[] args){
  73. Statistics stats = new Statistics();
  74. // The largest answer might be anything, but the average should be about 3.
  75. System.out.println(stats.avgKidsToBoth(1000000));
  76. // Should be about 2.5
  77. System.out.println(stats.avgKidsToTeam(2,1000000));
  78. // Should be about 4.12
  79. System.out.println(stats.avgKidsToTeam(3,1000000));
  80. // Should be about 5.81
  81. System.out.println(stats.avgKidsToTeam(4,1000000));
  82. // Should be about 7.53
  83. System.out.println(stats.avgKidsToTeam(5,1000000));
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement