Total number of families = 100000
Probability of having a boy or girl is same, that mean
chances of having girl is 50% = 0.50
chances of having boy = 1 - chance of having girl = 0.50
Using R Sample function lets solve
1. Average number of children there would be in a family if all people had children until they had a boy
options(scipen = 10, digits = 0)
gender <- c('B','G')
#set probabilities
prob.girl = 0.5
prob.boy = 1 - prob.girl
#number of families
n = 100000
#initiate a vector family with no children of size n
family <- rep(0, n)
i = 1
while (i <= n){
#get gender of the child
child <- sample(gender, size = 1, replace = TRUE, prob = c(prob.boy, prob.girl))
#add a child to a family
family[i] <- family[i] + 1
#if gender of the child is boy advance to next family
if (child == 'B'){
i = i + 1
}
}
#get number of children between 100000 famalies
total.children1 <- sum(family)
total.children1
## [1] 199577
#get average children between 100000 famalies
avg.children1 = round(total.children1/n,0)
avg.children1
## [1] 2
If every family had a child until they had a boy, there will be total of 199577 children.
Average number of children per family: 2
2. Do the same if all people had children until they had at least one boy and at least one girl
options(scipen = 10, digits = 0)
gender <- c('G','B')
#set probabilities
prob.girl = 0.5
prob.boy = 1 - prob.girl
#number of families
n = 100000
#initiate a matrix family with no children of size nX2
family <- matrix(0, nrow=100000, ncol=2)
#it is not necessary to name the columns
#values can be identified by family[1,1] for girl (first row, first column)
#similarly family[1,2] for boy (first row, second column)
#they are named in this case for easy readability
colnames(family) <- c('G', 'B')
i = 1
while (i <= n){
child <- sample(gender, size = 1, replace = TRUE, prob = c(prob.boy, prob.girl))
#increment child count based on gender
if (child == 'G'){
family[i,'G'] <- family[i,'G'] + 1
}
else{
family[i,'B'] <- family[i,'B'] + 1
}
#once family have a girl and a boy move to next family
if ((family[i,'G'] > 0) & (family[i,'B'] > 0)){
i = i + 1
}
}
#get number of children between 100000 famalies
total.children2 <- sum(family[,'G']) + sum(family[,'B'])
total.children2
## [1] 300560
#get average children between 100000 famalies
avg.children2 = round(total.children2/n,0)
avg.children2
## [1] 3
If every family had a child until they had at least one boy and at least one girl, there will be total of 300560 children.
Average number of children per family: 3
Under the second scheme there would be 100983 more children. And on an average, each family will have 1 extra child.
Note: Since set.seed function is not used in R code, numbers will vary each time code is run.