problem with probability program

hello. i am trying to prove the answer to a problem using the computer. the problem is "if a couple with 2 children has 1 daughter, what is the possibility that the second child is a boy". I tried making a program that randomly assigns the children of a couple.it should return an answer of around 2/3 but the problem is that it only returns an answer of either 1 or 0 (which would mean that the second child would always be a boy, or that it would never be a boy respectively). anyways, this is my code. what did i do wrong?

class siblings headers
1
2
3
4
5
6
7
8
9
10
11
class siblings
{
      public:
             siblings();
             void generate();//randomly assigns siblings
             int result();//returns 1 if one sibling is a brother
             void print();
      private:
              int sib1;
              int sib2;
};


class siblings definitions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "siblings.h"
#include <iostream>
#include <time.h>
#include <cstdlib>


siblings::siblings()
{
 sib1 = 1;
 sib2 = 1;
}

void siblings::generate()
{
     while ( sib1 == sib2 && sib1 == 1)//loop to keep reassigning children if both are boys, as the problem requires at least 1 girl
     {
     srand( time(0) );
     sib1 = rand() % 2;//should randomly assign 0 or 1 to sib1
     sib2 = rand() % 2;//should randomly assign 0 or 1 to sib2
     }
};

int siblings::result()//returns 1 if one of the siblings is a boy
{
     if (sib1 == 1 || sib2 == 1)
        return 1;
     else 
        return 0;
};

void siblings::print()
{
     std::cout << sib1 << " " << sib2 << std::endl;
}


Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "siblings.h"
using namespace std;

int main()
{
    double brother = 0.0;
    siblings fam1;
    double rest = 0.0;
    for (int x = 1; x <= 10000; x++)
     {
          fam1.generate();
          if ( fam1.result() == 1)
          {
             brother = brother + 1;
          }
     }
     rest = brother / 10000.0;
     cout << "possibility = " << rest << endl; 
     fam1.print();
     system("pause");
     return 0;
}
srand( time(0) );

This should only be called once, a reasonable place is the beginning of main().

I don't know about your logic though, seems like your program does "If there is a male child, increase the brother count".
Last edited on
removed srand from generate function and placed in beginning of main, still the problem persists.
it seems like it's only generating one set and using that for all 10000 runs despite the generate function being inside the loop.
the logic is as you said, every time a family with 1 male child is generated the brother count should increase.
while ( sib1 == sib2 && sib1 == 1)//loop to keep reassigning children if both are boys, as the problem requires at least 1 girl

Once sib1 and sib2 are values that satisfy the condition, they are never changed again, thus either every family has a boy or none have a boy.

I'm a little confused why you would keep generating random numbers in generate until you get what you want. If one must be a girl, then make one a girl. In fact, why even bother storing two siblings if you're only dealing with one being random? Just store the random one and assume the other is a girl, or better yet, don't store one at all.

1
2
3
4
int siblings::generate()
{
    return rand()%2 ;
}


Of course, that might reveal a flaw in your reasoning about the expected probability.
Last edited on
Once sib1 and sib2 are values that satisfy the condition, they are never changed again, thus either every family has a boy or none have a boy.

thanks, i changed it a do-while loop instead to ensure it is changed at least once every time the function is called
1
2
3
4
5
6
7
8
9
void siblings::generate()
{
     do
     {
     sib1 = rand() % 2;//should randomly assign 0 or 1 to sib1
     sib2 = rand() % 2;//should randomly assign 0 or 1 to sib2
     }
     while ( sib1 == sib2 && sib1 == 1);
};


I'm a little confused why you would keep generating random numbers in generate until you get what you want. If one must be a girl, then make one a girl. In fact, why even bother storing two siblings if you're only dealing with one being random?

they're both random because the order is not specified. the possibilities are boy-girl, girl-boy, and girl-girl. if i force either the first or second to be a girl then there are only 2 possibilities and it changes the problem. it is like the Monty hall problem in reverse. anyways, program runs okay now and result is as expected. thank you.
they're both random because the order is not specified.


what is the possibility that the second child is a boy


Second implies order to me. ;)
Second implies order to me. ;)

yea that's my bad, ambiguous wording. second wasn't meant to imply either one is the youngest or oldest, just that there are 2 and you know one of them. i guess a better way to say it would be "if one child is a girl, what is the possibility that the other is a boy".
Topic archived. No new replies allowed.