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;
};
#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;
elsereturn 0;
};
void siblings::print()
{
std::cout << sib1 << " " << sib2 << std::endl;
}
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.
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.
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".