如何调 random.choice 概率 - python -
How to tune random.choice probability - python -
我做了一个从文本列表中随机选择一个的函数。
def ttc(*arg):
a = random.choice([*arg])
return ''.join(a)
print(ttc("Price", "Condition", "Loan"))
print(ttc("entertainment", "geese", "Uruguay", "comedy", "butterfly"))
但我发现有必要让某些词被选中的概率更高
比如我们可以这样改变从列表前面依次选中的概率50% 30% 20%吗?
您可以简单地添加更多的单词您希望有更高的被选中的概率。
基本方式:
只需修改输入即可反映您的概率
["a","b","c"] vs ["a","a","a","b","c"] # 3 times more "a" then "b" or "c"
提供概率:
使用 random.choices
和 1 元素绘制和 provide a probability:
import random
# fixed - some unneeded stuff in here - no idea why ...
def ttc(*arg):
a = random.choice(arg) # no need to list and unpack
return a # no need to join here
def ttc2(words, probs, draws = 1):
a = random.choices(words, weights=probs, k = draws)
return a if draws == 1 else ' '.join(a)
for _ in range(5):
print(ttc("Price", "Condition", "Loan"))
# Price is roughly 20 times more likely
# use [5,3,2] to get 50% 30% 20%
# print 30 draws of that probability
print( ttc2( ["Price", "Condition", "Loan"], [20,1,1], 30))
输出:
Loan
Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Condition Price Price Price Price Price Price Loan Price Price Price Price Price
Price
Price Price Price Price Price Price Price Condition Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Condition Price Price Price Price
Price
Price Price Price Price Price Price Price Price Price Price Price Price Loan Price Price Condition Price Price Price Price Price Price Price Price Price Price Price Price Condition Price
Condition
Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Condition Price Loan Price Price Price Price Price Price Price Price Price Price
Loan
Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Condition Price Price Price
我做了一个从文本列表中随机选择一个的函数。
def ttc(*arg):
a = random.choice([*arg])
return ''.join(a)
print(ttc("Price", "Condition", "Loan"))
print(ttc("entertainment", "geese", "Uruguay", "comedy", "butterfly"))
但我发现有必要让某些词被选中的概率更高
比如我们可以这样改变从列表前面依次选中的概率50% 30% 20%吗?
您可以简单地添加更多的单词您希望有更高的被选中的概率。
基本方式:
只需修改输入即可反映您的概率
["a","b","c"] vs ["a","a","a","b","c"] # 3 times more "a" then "b" or "c"
提供概率:
使用 random.choices
和 1 元素绘制和 provide a probability:
import random
# fixed - some unneeded stuff in here - no idea why ...
def ttc(*arg):
a = random.choice(arg) # no need to list and unpack
return a # no need to join here
def ttc2(words, probs, draws = 1):
a = random.choices(words, weights=probs, k = draws)
return a if draws == 1 else ' '.join(a)
for _ in range(5):
print(ttc("Price", "Condition", "Loan"))
# Price is roughly 20 times more likely
# use [5,3,2] to get 50% 30% 20%
# print 30 draws of that probability
print( ttc2( ["Price", "Condition", "Loan"], [20,1,1], 30))
输出:
Loan
Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Condition Price Price Price Price Price Price Loan Price Price Price Price Price
Price
Price Price Price Price Price Price Price Condition Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Condition Price Price Price Price
Price
Price Price Price Price Price Price Price Price Price Price Price Price Loan Price Price Condition Price Price Price Price Price Price Price Price Price Price Price Price Condition Price
Condition
Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Condition Price Loan Price Price Price Price Price Price Price Price Price Price
Loan
Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Price Condition Price Price Price