这样是否会导致男女比例失衡?

网上谈论到当今社会男女比例失调，有人说到一个问题，某些地方政策是，夫妻俩生育一个孩子后，如果是女的就可以再生一个，如果是男的，就不能继续了。这导致了男女比例失调。然而，立马有人反对说，这件事对全社会影响比例非常小。

Argue is cheap, show me the code!

我做了一个简单的模拟器，模拟这个事情，看结果就完了。

具体设定如下：

每个人到20岁开始寻找伴侣，到60生命结束

新生儿男女比例都是1/2

生下女儿则两年后继续尝试，生下儿子则停止尝试(在此，我假定生育完一个孩子后要隔一年再来战过。)

如果生下五个女儿，也停止尝试(当然，如果夫妻有人死了自然不能继续生育了)

假定理想条件，年纪大优先找到另一半，但45岁后不再尝试

如此模拟200年(这个时间我随便写的，修改个变量就是2000年也很容易)，输出:

终身未娶/未嫁和成功完婚的数量

在世的男女比例

在世未婚的男女比例

为了代码方便，使用了这样一些假定如下以hard code放里面了。

假定开始时间是2014年

开始时刚出生的男女人数相同

模拟器构造了几个队列，分别表示男孩，女孩，夫妻和FA的筒子们，每年对生孩子，结婚和死亡分别统计。生下的孩子性别用随机数roll个，每年统计下各个队列里面的信息即可。构造非常简单，它对我的最大困扰其实是自己手工实现个链式队列。

那么，你觉得这个政策影响大么？

附：直接给一个相关代码和参考结果吧，你看符合你预期吧。

import random
from collections import deque
import matplotlib.pyplot as plt
from tqdm import tqdm

class Person:
"""表示一个人的类，包含性别、出生年份、婚姻状态等属性"""

def __init__(self, gender, birth_year):
self.gender = gender # 'M' for male, 'F' for female
self.birth_year = birth_year
self.married = False
self.spouse = None # 配偶引用
self.children = 0 # 孩子总数
self.last_child_year = None # 最后一个孩子出生的年份
self.daughters = 0 # 女儿数量

def age(self, current_year):
"""计算当前年龄"""
return current_year - self.birth_year

def is_alive(self, current_year):
"""判断是否在世（假设60岁为寿命上限）"""
return self.age(current_year) = 20
and self.age(current_year) 0 and self.daughters = 5:
return False

# 需要在上一个孩子出生后等待2年
if self.last_child_year is not None and current_year - self.last_child_year < 2:
return False

# 女性年龄限制（20-45岁）
if self.gender == "F" and (
self.age(current_year) 45
):
return False

return True

class Couple:
"""表示一对夫妻的类"""

def __init__(self, husband, wife, marriage_year):
self.husband = husband
self.wife = wife
self.marriage_year = marriage_year
# 更新双方的婚姻状态
husband.married = True
husband.spouse = wife
wife.married = True
wife.spouse = husband

def is_active(self, current_year):
"""判断夫妻双方是否都在世"""
return self.husband.is_alive(current_year) and self.wife.is_alive(current_year)

def can_have_children(self, current_year):
"""判断夫妻是否可以生育（委托给妻子判断）"""
return self.wife.can_have_children(current_year)

def have_child(self, current_year):
"""生育一个孩子，50%概率为男孩或女孩"""
# 50% chance for each gender
gender = "M" if random.random() = 45 and not m.married:
self.males.remove(m)
self.forever_alone_males.append(m)
self.historical_forever_alone_males += 1

for f in list(self.females):
if f.age(self.current_year) >= 45 and not f.married:
self.females.remove(f)
self.forever_alone_females.append(f)
self.historical_forever_alone_females += 1

def process_births(self):
"""处理生育"""
for couple in self.couples:
if couple.can_have_children(self.current_year):
child = couple.have_child(self.current_year)
if child.gender == "M":
self.males.append(child)
self.total_males_ever += 1
else:
self.females.append(child)
self.total_females_ever += 1

def collect_stats(self):
"""收集年度统计数据"""
# 计算总人口
total_males = (
len(self.males)
+ len(self.forever_alone_males)
+ sum(1 for c in self.couples if c.husband.is_alive(self.current_year))
)
total_females = (
len(self.females)
+ len(self.forever_alone_females)
+ sum(1 for c in self.couples if c.wife.is_alive(self.current_year))
)

# 计算未婚人口
unmarried_males = len(self.males) + len(self.forever_alone_males)
unmarried_females = len(self.females) + len(self.forever_alone_females)

# 存储统计数据
self.yearly_stats.append(
{
"year": self.current_year,
"total_males": total_males,
"total_females": total_females,
"unmarried_males": unmarried_males,
"unmarried_females": unmarried_females,
"couples": len(self.couples),
"forever_alone_males": len(self.forever_alone_males),
"forever_alone_females": len(self.forever_alone_females),
}
)

def display_results(self):
"""显示最终结果"""
final_stats = self.yearly_stats[-1]

print("\n=== Final Statistics ===")
print(f"Year: {final_stats['year']}")
print(
f"Total population: {final_stats['total_males'] + final_stats['total_females']}"
)
print(
f"Gender ratio (M:F): {final_stats['total_males']} : {final_stats['total_females']} = {final_stats['total_males'] / final_stats['total_females']:.2f}"
)
print(
f"Unmarried gender ratio (M:F): {final_stats['unmarried_males']} : {final_stats['unmarried_females']} = {final_stats['unmarried_males'] / final_stats['unmarried_females'] if final_stats['unmarried_females'] > 0 else 'inf':.2f}"
)
print(f"Forever alone males: {final_stats['forever_alone_males']}")
print(f"Forever alone females: {final_stats['forever_alone_females']}")
print(f"Active couples: {final_stats['couples']}")

# 新增：输出历史总人口及比例
print("\n--- Historical Population (All Ever Born) ---")
print(f"Total males ever born: {self.total_males_ever}")
print(f"Total females ever born: {self.total_females_ever}")
if self.total_females_ever > 0:
print(f"Historical gender ratio (M:F): {self.total_males_ever} : {self.total_females_ever} = {self.total_males_ever / self.total_females_ever:.2f}")
else:
print("Historical gender ratio (M:F): inf")
print(f"Historical Forever Alone Males: {self.historical_forever_alone_males}")
print(f"Historical Forever Alone Females: {self.historical_forever_alone_females}")

# 绘制性别比例随时间变化的图表
self.plot_results()

def plot_results(self):
"""绘制结果图表"""
years = [stat["year"] for stat in self.yearly_stats]
gender_ratios = [
(
stat["total_males"] / stat["total_females"]
if stat["total_females"] > 0
else 0
)
for stat in self.yearly_stats
]
unmarried_ratios = [
(
stat["unmarried_males"] / stat["unmarried_females"]
if stat["unmarried_females"] > 0
else 0
)
for stat in self.yearly_stats
]

plt.figure(figsize=(12, 6))

# 绘制总体性别比例图
plt.subplot(1, 2, 1)
plt.plot(years, gender_ratios, "b-", label="Gender Ratio (M:F)")
plt.axhline(y=1.0, color="r", linestyle="--", label="Equal Ratio")
plt.title("Gender Ratio Over Time")
plt.xlabel("Year")
plt.ylabel("Ratio (M:F)")
plt.legend()

# 绘制未婚人口性别比例图
plt.subplot(1, 2, 2)
plt.plot(years, unmarried_ratios, "g-", label="Unmarried Ratio (M:F)")
plt.axhline(y=1.0, color="r", linestyle="--", label="Equal Ratio")
plt.title("Unmarried Gender Ratio Over Time")
plt.xlabel("Year")
plt.ylabel("Ratio (M:F)")
plt.legend()

plt.tight_layout()
plt.savefig("gender_ratio_simulation.png")
plt.show()

if __name__ == "__main__":
# 运行模拟
sim = Simulation(initial_population=10000, years=200, start_year=2014)
sim.run()

参考结果：

=== Final Statistics ===
Year: 2214
Total population: 14688
Gender ratio (M:F): 7330 : 7358 = 1.00
Unmarried gender ratio (M:F): 2383 : 2411 = 0.99
Forever alone males: 0
Forever alone females: 0
Active couples: 4947

--- Historical Population (All Ever Born) ---
Total males ever born: 32943
Total females ever born: 32884
Historical gender ratio (M:F): 32943 : 32884 = 1.00
Historical Forever Alone Males: 1354
Historical Forever Alone Females: 1263