## Welcome to BrainDen.com - Brain Teasers Forum

 Welcome to BrainDen.com - Brain Teasers Forum. Like most online communities you must register to post in our community, but don't worry this is a simple free process. To be a part of BrainDen Forums you may create a new account or sign in if you already have an account. As a member you could start new topics, reply to others, subscribe to topics/forums to get automatic updates, get your own profile and make new friends. Of course, you can also enjoy our collection of amazing optical illusions and cool math games. If you like our site, you may support us by simply clicking Google "+1" or Facebook "Like" buttons at the top. If you have a website, we would appreciate a little link to BrainDen. Thanks and enjoy the Den :-)
Guest Message by DevFuse

34 replies to this topic

### #31 phillip1882

phillip1882

Senior Member

• Members
• 765 posts

Posted 21 June 2011 - 07:31 PM

i seem to be getting 1/2.
```import random
count1 = 0
count2 = 0
for i in range(0,100000):
value1 = random.randint(0,1)
if value1 == 1: #if they answer yes to "do you have a daughter" (if they answer no to this then we dont consider it.)
count1 += 1
value2 = random.randint(0,1)
if value2 == 0: #if they answer no to "do you have a son"
count2 += 1

print(count2/count1)
```
gives me .4996.
it's all in how you interpret it.
of course, if i wrote the program...
```for i in range(0,100000):
value1 = random.randint(0,1)
value2 = random.randint(0,1)
if value1 == 1:
count1 += 1
if value2 == 0:
count2 += 1
```
then it would be 1/3.
• 0

### #32 superprismatic

superprismatic

Not just Prismatic

• Moderator
• 1281 posts
• Gender:Male

Posted 21 June 2011 - 08:00 PM

i seem to be getting 1/2.

```import random
count1 = 0
count2 = 0
for i in range(0,100000):
value1 = random.randint(0,1)
if value1 == 1: #if they answer yes to "do you have a daughter" (if they answer no to this then we dont consider it.)
count1 += 1
value2 = random.randint(0,1)
if value2 == 0: #if they answer no to "do you have a son"
count2 += 1

print(count2/count1)
```
gives me .4996.
it's all in how you interpret it.
of course, if i wrote the program...
```for i in range(0,100000):
value1 = random.randint(0,1)
value2 = random.randint(0,1)
if value1 == 1:
count1 += 1
if value2 == 0:
count2 += 1
```
then it would be 1/3.

No, you don't flip the coins to answer the questions -- you flip them to decide
the genders of both children. Then, and only then, do you fill out the
questionnaires. In reality, the children would already exist before you got
the questionnaire, so you have to decide about both children first in order
to accurately simulate the situation.
• 0

### #33 phillip1882

phillip1882

Senior Member

• Members
• 765 posts

Posted 21 June 2011 - 08:31 PM

but again, if i do...
```import random
total1girl = 0
total2girl = 0
child = [-1]*2
for i in range(0,100000):
for j in range(0,2):
child[j] = random.randint(0,1)
if child[0] == 1 or child[1] == 1: #if either the first or second child is a girl
total1girl += 1
if child[0] == 1 and child[1] == 1: #if both childern are girls
total2girl += 1

print(total2girl/total1girl)
```
then i of course will get 1/3.
if however i do...
```for i in range(0,100000):
for j in range(0,2):
child[j] = random.randint(0,1)
if child[0] == 1: #if the first child is a girl.
total1girl += 1
if child[1] == 0: #if the second child is a girl.
total2girl += 1
```
then its 50/50.

Edited by phillip1882, 21 June 2011 - 08:32 PM.

• 0

### #34 superprismatic

superprismatic

Not just Prismatic

• Moderator
• 1281 posts
• Gender:Male

Posted 21 June 2011 - 08:54 PM

but again, if i do...

```import random
total1girl = 0
total2girl = 0
child = [-1]*2
for i in range(0,100000):
for j in range(0,2):
child[j] = random.randint(0,1)
if child[0] == 1 or child[1] == 1: #if either the first or second child is a girl
total1girl += 1
if child[0] == 1 and child[1] == 1: #if both childern are girls
total2girl += 1

print(total2girl/total1girl)
```
then i of course will get 1/3.
if however i do...
```for i in range(0,100000):
for j in range(0,2):
child[j] = random.randint(0,1)
if child[0] == 1: #if the first child is a girl.
total1girl += 1
if child[1] == 0: #if the second child is a girl.
total2girl += 1
```
then its 50/50.

You're still cutting corners. Simulations should carry out every step of
the process as closely as possible. In this case, flip the coins to make 2
kids, then answer the two questions "Do you have a daughter?" and "Do you
have a son?" and make a large bunch of questionnaires this way. Then, take
the questionnaires that have a "yes" answer to "Do you have a daughter?" and
count the proportion of them with a "no" answer to "Do you have a son?".
That second program seems to make distinctions (first child -- second child)
which the questionnaire doesn't address at all. To be REALLY convincing,
a simulation must carry out every step of what it is trying to simulate.
Every programming shortcut must be avoided. So, I don't think even the first
program qualifies as a good simulation of the questionnaire scenario.
• 0

### #35 fabpig

fabpig

Prominent Member

• Members
• 2423 posts
• Gender:Male
• Location:Cumbria, England

Posted 23 June 2011 - 01:17 AM

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
• 0

You must not think me necessarily foolish because I am facetious,  nor will I consider you necessarily wise because you are grave.  Sydney Smith.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users