简体   繁体   English

尽管随机组件,我多次调用函数时Python会给出相同的结果

[英]Python giving same result when I call a function multiple times, despite random component

I am writing a baseball game simulation. 我正在写一个棒球比赛模拟。 I would like to be able to run multiple games to see how different batting averages affect outcomes. 我希望能够运行多个游戏,看看不同的击球平均值如何影响结果。 Each game is made up of "at-bats", the result of which comes from a random number. 每个游戏都由“蝙蝠”组成,其结果来自随机数。

The issue is that when I go to run multiple games, I get the same result for every game. 问题在于,当我去运行多个游戏时,每个游戏都会得到相同的结果。

I imagine Python is remembering the result of the function, and just using that. 我想Python正在记住函数的结果,只是使用它。 I am new to Python/CS, and so have been trying to look up issues of memory, etc, but am not finding the answers I need. 我是Python / CS的新手,所以一直试图查找内存等问题,但我找不到我需要的答案。 I appreciate any help or direction to a resource I would appreciate it. 我感谢任何帮助或对资源的指导我会很感激。 Thank you 谢谢

Following is a simplified version in order to help me explain the problem. 以下是简化版本,以帮助我解释问题。 It uses just hits and outs, ending a game at 27 outs. 它只使用了点击率,结束了27场比赛。 At the end it loops through five games. 最后它循环了五场比赛。

  import random

  hits = 0
  outs = 0

  # determine whether player (with .300 batting average) gets a hit or an out
  def at_bat():
     global hits
     global outs
     number = random.randint(0,1000)
     if number < 300:
        hits +=1
     else:
        outs += 1

  # run at_bat until there are 27 outs
  def game():
      global hits
      global outs
      while outs < 27:
         at_bat()
      else:
         print "game over!"
         print hits

  # run 5 games
  for i in range(0,5):
     game()

The problem lies in your use of global variables. 问题在于你使用全局变量。

After your game() has run for the first time, outs is 27. When you call game again, it still has the same value, so your while loop exits immediately. 在游戏()第一次运行后,输出为27.当您再次呼叫游戏时,它仍具有相同的值,因此您的while循环立即退出。

import random

# determine whether player (with .300 batting average) gets a hit or an out
def game():
    global hits
    global outs
    hits = 0
    outs = 0
    while outs < 27:
        hits, outs = at_bat()
    else:
        print("game over!")
        print(hits)

def at_bat():
    global hits
    global outs
    number = random.randint(0,1000)
    if number < 300:
        hits += 1
    else:
        outs += 1
    return hits, outs

  # run 5 games
for i in range(0,5):
    game()

I have always found global to mess up sometimes but this code works and gets you different numbers. 我总是发现全局有时会陷入困境,但这段代码可以运行并为您提供不同的数字。 outs would always be 27 each time your game code ran, resetting them to 0 ensures your game loop will run each time 每次游戏代码运行时, outs总是27,将它们重置为0可确保每次运行游戏循环

Ah, the global variables headache... 啊,全球变数令人头疼......

Actually, your code would work well if you reset those two globals for each loop. 实际上,如果为每个循环重置这两个全局变量,您的代码将运行良好。 So: 所以:

for i in range(0,5):
    game()
    hits = 0
    outs = 0

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

相关问题 将函数的结果复制为类变量或多次调用它 - Copy the result of a function as a class variable or call it multiple times Python Python / Selenium:多次调用函数时出现ERRNO 111 - Python / Selenium : ERRNO 111 when I call my function multiple times Python:使用相同的参数多次调用相同的函数,还是将结果保存为中间值? - Python: call the same function with the same arguments several times, or save the result as an intermediate value? 多次调用同一函数时,Python设计模式更好 - Better Python Design Pattern when calling same function multiple times 尽管 Python 中的输入相同,但 Function 返回不同的结果 - Function returning different result despite the same inputs in Python 在python中,如何多次调用类并使randint随机? - In python, how do I call a class multiple times and have randint be random? 如何在 Python 中多次调用 function? - How to call a function multiple times in Python? Python装饰器调用函数多次 - Python decorator call function multiple times 如何多次使用函数结果作为同一函数的参数? - How to use result of function as an argument of the same function multiple times? 在同一个函数中多次返回一个函数的结果 - returning result of a function multiple times within the same function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM