Select 个子列表,其中每个元素都有 1.5% 的几率被包含

Select sublist where each element has a 1.5% chance to be included

     tmp = []
     while len(tmp) == 0:
         for i in range(0,385):
             # Multiplying by 100 in order to remove the decimal point
             if randint(0,10000) < chance*100:
                 tmp.append(i)
     return tmp

这是我目前用来帮助解决问题的代码。输出将是这样的。

[34, 234, 243, 321]

我目前的解决方案效率很低。我试过这个:

sample(range(0,385), math.ceil(chance*3.85))

但它不会产生相同的效果。另外,如果你能告诉我这个叫什么的名字那就太好了。

import numpy as np

n = 385
chance = 0.015  # Chance of 1.5%
main_list = np.arange(n)  # Generate initial list
rnd = np.random.uniform(size=main_list.shape)  # Generate random number between 0 and 1

sublist = main_list[rnd < chance]  # Select numbers