My 2 cents worth:
I wrote these in AutoHotKey to try and give my 2 teenage children (both girls!) an idea of how a computer program works. To prove a point I included a breakdown of no. of boys from the same sample.
The first is easier to read and understand (I hope), the second is about 25% faster. I apologise if the ;comments are a bit obvious/patronising
#NoEnv
SendMode Input
;set counters to 0
atleastonegirl = 0, botharegirls = 0, atleastoneboy = 0, bothareboys = 0
loop 10000000
{
random, child1, 0, 1 ; randomly assign 1 (girl) or 0 (boy) to child1
random, child2, 0, 1 ; likewise child2
if (child1 or child2 ) ; if either are girls
{
atleastonegirl ++ ; add one to counter
if (child1 and child2) ; if both are girls
botharegirls ++ ; add one to counter
}
if (!child1 or !child2) ; if either are boys
{
atleastoneboy ++ ; add one to counter
if (!child1 and !child2) ; if both are boys
bothareboys ++ ; add one to counter
}
}
girlpercent := botharegirls / atleastonegirl * 100
boypercent := bothareboys / atleastoneboy * 100
; Write to file
FILEAPPEND, 10000000 - sample of 2 children families`n %atleastonegirl% - no. who have at least one girl. `n %botharegirls% - no. who have both girls`n`n %girlpercent% percent `n`n`n%A_SPACE%, %A_MYDOCUMENTS%\famsample.txt
;
FILEAPPEND, %atleastoneboy% - no. who have at least one boy. `n %bothareboys% - no. who have both boys`n`n %boypercent% percent `n, %A_MYDOCUMENTS%\famsample.txt
msgbox Done
result:
10000000 - sample of 2 children families
7500583 - no. who have at least one girl.
2501412 - no. who have both girls
33.349568 percent
7498588 - no. who have at least one boy.
2499417 - no. who have both boys
33.331835 percent
#NoEnv
SendMode Input
;set counters to 0
atleastonegirl = 0, botharegirls = 0, atleastoneboy = 0, bothareboys = 0
loop 10000000
{
random, child1, 0, 1 ; randomly assign 1 (girl) or 0 (boy) to child1
random, child2, 0, 1 ; likewise child2
if (child1 or child2) ; if either are girls
{
atleastonegirl ++ ; add one to counter
if (child1 and child2) ; if both are girls
botharegirls ++ ; add one to counter
else
atleastoneboy ++ ; if one is a girl but not both, then one is a boy
}
else
{
bothareboys ++ ; if neither are girls, both are boys
atleastoneboy ++ ; and at least one is, too!
}
}
girlpercent := botharegirls / atleastonegirl * 100
boypercent := bothareboys / atleastoneboy * 100
; Write to file
FILEAPPEND, 10000000 - sample of 2 children families`n %atleastonegirl% - no. who have at least one girl. `n %botharegirls% - no. who have both girls`n`n %girlpercent% percent `n`n`n%A_SPACE%, %A_MYDOCUMENTS%\famsample.txt
;
FILEAPPEND, %atleastoneboy% - no. who have at least one boy. `n %bothareboys% - no. who have both boys`n`n %boypercent% percent `n, %A_MYDOCUMENTS%\famsample.txt
msgbox Done
result:
10000000 - sample of 2 children families
7500527 - no. who have at least one girl.
2500002 - no. who have both girls
33.331018 percent
7499998 - no. who have at least one boy.
2499473 - no. who have both boys
33.326316 percent