男女出生比例程序模拟c++

在这里插入图片描述
模拟:男女出生概率相等的情况下,家庭偏爱生男孩
结果:出生率只和出生概率有关,和生娃策略无关

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

long long male,female,n,t;
const int N=1e7;

int main()
{
	n=N;
	srand( (unsigned)time( NULL ) );
	rand();
	while(n--)
	{
		t=3;
		while(t--)
		{
			int tmp=rand();
			if(tmp%2==0){
				male++;
				break;
			}
			else female++;
		}
	}
	cout<<"male:"<<male<<endl;
	cout<<"female:"<<female<<endl;
	return 0;
} 
posted @ 2022-11-17 23:04  林动  阅读(68)  评论(0编辑  收藏  举报