如果我有python中指定的varous范围的概率,我怎么能生成随机数

Sag*_*gia 5 random numpy probability python-3.x

我想填写出勤的虚拟数据.我希望如此,例如,60%的学生在40-60范围内的出勤率为70-100%,在0-40岁的范围内为15%.如何在Python中使用随机数生成此项.这有什么内置功能吗?我知道numpy.random.choice允许预定义离散数的概率,但有没有办法指定容器/范围的概率?

Gia*_*chi 0

如果你知道学生人数N,你可以

N_ha = int(N * 0.6)  # students with high attendance
N_la = int(N * 0.15)  # students with low attendance
N_aa = N - ha - la  # students with average attendance

att_ha = np.random.random(N_ha) * 0.3 + 0.7  # this creates N_ha attendances in the half-open range [0.7, 1)
att_la = np.random.random(N_la) * 0.4
att_aa = np.random.random(N_aa) * 0.2 + 0.4  # sure you didn't mean between 40 and 70? in that case, substitute 0.2 with 0.3

attendances = x = np.append(att_ha, np.append(att_la, att_aa))
np.random.shuffle(attendances)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!