So I have this random number generator program but I don't know how to change it from stopping after generating the certain number once to continuing to run for x amount of times and then displaying how many times it generated that certain number within the amount of times I wanted it to run.
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))
eg I would want it to run 1000 times and then tell me how many times it rolled the number 1 when generating numbers between 1 and 64.
This is just for personal use to simulate a drop table from a video game.
Don't modify this program that does not do what you need; write a program that does what you need.
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(...))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.