python中基于百分比的随机

python中基于百分比的随机,python,Python,我有一个列表,[“a”、“b”、“c”],其中“a”应代表85%的产出,“b”应代表10%的产出,“c”应代表5% 我想打印一个数组,它的大小有这些百分比,并且数组大小是可变的 例如:如果输入大小为20,那么数组中的“a”应该出现17次,“b”应该出现2次,“c”应该出现1次 有什么想法吗?如果我能很好地回答你的问题,我会先问: 输出编号 您希望这三个字母代表的百分比(除非固定为85-10-5) 然后继续将所述输入数字除以100并乘以所需百分比,为每个字母启动for循环,填充数组(数组大小将

我有一个列表,
[“a”、“b”、“c”]
,其中“a”应代表85%的产出,“b”应代表10%的产出,“c”应代表5%

我想打印一个数组,它的大小有这些百分比,并且数组大小是可变的

例如:如果输入大小为20,那么数组中的“a”应该出现17次,“b”应该出现2次,“c”应该出现1次


有什么想法吗?

如果我能很好地回答你的问题,我会先问:

  • 输出编号
  • 您希望这三个字母代表的百分比(除非固定为85-10-5)
然后继续将所述输入数字除以100并乘以所需百分比,为每个字母启动for循环,填充数组(数组大小将保持可变)

为了说明我在Python3中对您的问题的理解:

print("please type an integer, then Enter : ")
# the following integer will be the size of your array. Here I chose to prompt the user so that you can execute and see for yourself that you can change it as you like and the solution will stay valid. 
startingInteger = int(input())
print(f"the startingInteger is {startingInteger}")

print("what is the percentage of a?")
aPercentage = int(input())
print("what is the percentage of b?")
bPercentage = int(input())
print("what is the percentage of c?")
cPercentage = int(input())

array = []

for i in range(int(round(startingInteger/100*aPercentage))):
  array.append("a")

for i in range(int(round(startingInteger/100*bPercentage))):
  array.append("b")

for i in range(int(round(startingInteger/100*cPercentage))):
  array.append("c")

print(array)

然后,您需要对结果数组进行加扰(如果这就是您所说的“random”)如果导入“random”模块并使用加扰()的话,这应该不会造成问题。这是一个快速、肮脏和不雅的问题,但这样的逻辑非常明确,希望能说明这一点。

你的百分比加起来是105%。我编辑了这个问题,它们应该是85%、10%和5%。请提供一个具体的例子,这个问题很模糊。如果不清楚,请告诉我:)如果你想要准确,用所需字母填充数组,然后将其洗牌。如果您希望它是随机的,请创建一个CDF,例如,如果[a,B,C]的百分比为[0.1,0.75,0.05],则使用[0.1,0.85,1]并选择一个随机数,并计算出它小于CDF的哪个元素(使用对分来计算日志n搜索时间)。这只适用于一个分布,并且仅适用于
'a',B',C'
。在第二个
print
语句中,您遗漏了f字符串中的
f
。你不应该逐字逐句地解决这个问题。我认为你是对的,我对我的答案进行了编辑,使其范围更广。同样,除以20而不是100意味着乘以期望的百分比/5,并且没有增加任何清晰度。