简体   繁体   English

Python-如何使该RNG程序在生成数字一次后继续运行x次

[英]Python - How do i make this RNG program continue to run for x amount of times after generating the number once

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. 因此,我有这个随机数生成器程序,但是我不知道如何将其从生成特定数字一次后停止到继续运行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))

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. 例如,我希望它运行1000次,然后告诉我在生成1到64之间的数字时将数字1滚动了多少次。

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(...))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM