Initial Commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import random
|
||||
|
||||
PRIZES = [
|
||||
{"rank": 1, "name": "1st Prize", "miles": 10000, "probability": 0.20},
|
||||
{"rank": 2, "name": "2nd Prize", "miles": 5000, "probability": 0.30},
|
||||
{"rank": 3, "name": "3rd Prize", "miles": 2000, "probability": 0.50},
|
||||
]
|
||||
|
||||
def draw_prize():
|
||||
rand_val = random.random()
|
||||
cumulative = 0.0
|
||||
for prize in PRIZES:
|
||||
cumulative += prize["probability"]
|
||||
if rand_val <= cumulative:
|
||||
return prize
|
||||
return PRIZES[-1]
|
||||
|
||||
counts = {1: 0, 2: 0, 3: 0}
|
||||
trials = 100000
|
||||
|
||||
for _ in range(trials):
|
||||
res = draw_prize()
|
||||
counts[res["rank"]] += 1
|
||||
|
||||
print("Results for 100,000 trials:")
|
||||
for rank, count in counts.items():
|
||||
print(f"Rank {rank}: {count/trials*100:.2f}%")
|
||||
Reference in New Issue
Block a user