使用Python随机生成的20个_Outcome()样品
发布时间:2023-12-11 09:43:22
生成20个_Outcome()样品带使用例子的代码如下:
import random
class Outcome:
def __init__(self, name, odds):
self.name = name
self.odds = odds
def __str__(self):
return self.name
outcomes = []
for i in range(20):
name = "Outcome " + str(i+1)
odds = random.randint(1, 10)
outcome = Outcome(name, odds)
outcomes.append(outcome)
sample_outcomes = random.sample(outcomes, 20)
print("Sample Outcomes:")
for outcome in sample_outcomes:
print(outcome)
以上代码创建了一个Outcome类,该类有两个属性name和odds,分别表示样本名称和概率。然后使用循环生成20个随机的Outcome样本,并将其存储在outcomes列表中。接着使用random.sample()函数从outcomes列表中随机选择20个样本,并将选择的样本存储在sample_outcomes列表中。最后,将选取的样本打印出来。
运行以上代码,可以得到一个类似以下的输出:
Sample Outcomes: Outcome 10 Outcome 8 Outcome 15 Outcome 1 Outcome 2 Outcome 6 Outcome 11 Outcome 14 Outcome 18 Outcome 4 Outcome 5 Outcome 16 Outcome 12 Outcome 13 Outcome 17 Outcome 20 Outcome 3 Outcome 9 Outcome 19 Outcome 7
这是20个随机选择的Outcome样本的列表。每个样本都有一个 的名称。
