[英]Python - How do i make this RNG program continue to run for x amount of times after generating the number once
因此,我有這個隨機數生成器程序,但是我不知道如何將其從生成特定數字一次后停止到繼續運行x次,然后顯示在一定數量范圍內生成該特定數字的次數,如何更改它。我想讓它運行。
from random import randint
attempts = 0
print("What would you like to test-roll today?")
print("(64, 128, 256, 384, 512, 1024, 5000")
inp = str(input())
lootTable = {"1024":1024,
"128":128,
"256":256,
"5000":5000,
"512":512,
"384":384,
"64":64}
while True:
rnd = randint(1,lootTable[inp])
attempts += 1
print("Rolling for %s (1/%d), attempt %d" %(inp, lootTable[inp], attempts))
if rnd == 1:
break
print("%s acquired on attempt %d!" %(inp, attempts))
例如,我希望它運行1000次,然后告訴我在生成1到64之間的數字時將數字1滾動了多少次。
這僅供個人用來模擬視頻游戲中的棄牌桌。
不要修改此程序,它不能滿足您的需求; 編寫一個滿足您需求的程序。
import random
def count_hits(attempts, max_random, number_to_hit):
# Every time the random generator hits the right number,
# add 1 to the sum. It will count the number of hits.
# The _ is just a variable about the value of which you don't care.
return sum(1 for _ in range(attempts)
if random.randint(1, max_random) == number_to_hit)
# Add a few input() calls, then print(count_hits(...))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.