如何在以下条件下选择带替换和权重的k
元素?
- 随机性必须是加密安全的,例如在
secrets
模块中使用的随机性。 - 权重必须精确,即使用整数而不是浮点运算。
自行编写的代码可能比可用的实现更不安全和更有效。据我所知,以下实现不符合我的要求。
random.choices()
numpy.random.choice()
pandas.DataFrame.sample()
如何在以下条件下选择带替换和权重的k
元素?
secrets
模块中使用的随机性。自行编写的代码可能比可用的实现更不安全和更有效。据我所知,以下实现不符合我的要求。
random.choices()
numpy.random.choice()
pandas.DataFrame.sample()
1条答案
按热度按时间xdnvmnnf1#
我会把choices实现从random模块中分离出来,比如:
其可以被测试为:
给了我:
看起来差不多。
更新:只是想检查一个错误,它似乎很好在这里:
给出:
这似乎也是对的