its_a_boy <- function(){
boy_born <- 0
total_children <- 0
while (boy_born < 1){
boy_girl <- runif(1)
total_children <- total_children+1
if (boy_girl > .5)
boy_born <- 2
}
return(total_children)
}
boy_and_girl <- function(){
other_sex <- 0
total_children <- 1
while (other_sex< 1){
boy_girl <- runif(1)
total_children <- total_children+1
if (boy_girl > .5)
other_sex <- 2
}
return(total_children)
}
set.seed(2)
a <- its_a_boy()
average_family_size_for_boy <- mean(replicate(100000, its_a_boy()))
average_family_size_for_boy_and_girl <- mean(replicate(100000,boy_and_girl()))
paste("family size to have a boy =",average_family_size_for_boy," family size to have a girl = ",average_family_size_for_boy_and_girl)
## [1] "family size to have a boy = 1.99419 family size to have a girl = 2.99472"
- As the 2nd function shows, I would expect a family size of one more.
- In theory, the sex of the first child does not matter in our second function(have a boy and a girl). No matter the first child, we are then looking for just a boy or just a girl, which is euqivalent to looking for just a boy in the first example