奇怪的行为与Python中的randint

huangapple go评论92阅读模式
英文:

strange behavior with randint in python

问题

这段代码有时会出现以下输出:

  1. value 1
  2. rol don't exist

为什么会出现这种行为以及存在哪些替代方法?

这种行为发生的原因是因为 rol_rand() 函数在每次调用时都会生成一个新的随机数,然后与 rol_dict 中的值进行比较。在两次调用 rol_rand() 之间,可能会生成不同的随机数,因此可能会导致不同的结果。

为了获得一致的结果,您可以在单个函数调用中生成随机数,并将其与 rol_dict 中的值进行比较,然后打印相应的结果。这样可以确保在同一次函数调用中进行比较,而不会出现不一致的结果。

以下是修复后的代码:

  1. rol_dict = {0: "value 1", 1: "value 2", 2: "value 3"}
  2. from random import randint # 导入随机数生成函数
  3. def rol_rand():
  4. return rol_dict.get(randint(0, 2))
  5. result = rol_rand()
  6. if result == rol_dict.get(0):
  7. print("value 1")
  8. elif result == rol_dict.get(1):
  9. print("value 2")
  10. elif result == rol_dict.get(2):
  11. print("value 3")
  12. else:
  13. print(result)
  14. print("rol don't exist")

这样,您可以确保随机数只在 rol_rand() 函数的单次调用中生成,从而获得一致的结果。

英文:
  1. rol_dict = { 0: "value 1", 1: "value 2", 2: "value 3" }
  2. def rol_rand():
  3. global rol_list
  4. return rol_dict.get(randint(0, 2))
  5. rol_rand()
  6. if rol_rand() == rol_dict.get(0):
  7. print("value 1")
  8. elif rol_rand() == rol_dict.get(1):
  9. print("value 2")
  10. elif rol_rand() == rol_dict.get(2):
  11. print("value 3")
  12. else:
  13. print(rol_rand())
  14. print("rol don't exist")

For this code some times appear this output

  1. value 1
  2. rol don't exist

why???

Why does this behavior occur and what alternatives exist?

答案1

得分: 1

问题在于你实际上调用了 rol_rand 多次,由于它每次返回一个随机数,因此不能保证一致性。

你的当前程序获取一个介于0和2之间的随机数。如果不是0,它会投掷一个新的随机数,然后检查它是否为1。如果不是,它会再次获取一个新的随机数并检查它是否为2。很可能你会得到3个与它们的检查不相对应的值。

为了解决这个问题,你只需要将投掷结果存储在一个变量中,像这样:

  1. from random import randint
  2. rol_dict = { 0: "值 1", 1: "值 2", 2: "值 3" }
  3. def rol_rand():
  4. return rol_dict.get(randint(0, 2))
  5. saved_roll = rol_rand()
  6. if saved_roll == rol_dict.get(0):
  7. print("值 1")
  8. elif saved_roll == rol_dict.get(1):
  9. print("值 2")
  10. elif saved_roll == rol_dict.get(2):
  11. print("值 3")
  12. else:
  13. print(saved_roll)
  14. print("投掷结果不存在")
英文:

The issue lies with the fact that you actually call rol_rand multiple different times, and since it returns a random number every time, there is not guaranteed consistency.

Your current program gets a random number between 0 and 2. If it's not 0, it rolls a new random number, checks if its 1. If not it gets a new random number again and checks if its 2. It's somewhat likely that you get 3 values that don't correspond to their checks.

In order to fix this, you just need to store the roll in a variable like so

  1. from random import randint
  2. rol_dict = { 0: "value 1", 1: "value 2", 2: "value 3" }
  3. def rol_rand():
  4. return rol_dict.get(randint(0, 2))
  5. saved_roll = rol_rand()
  6. if saved_roll == rol_dict.get(0):
  7. print("value 1")
  8. elif saved_roll == rol_dict.get(1):
  9. print("value 2")
  10. elif saved_roll == rol_dict.get(2):
  11. print("value 3")
  12. else:
  13. print(saved_roll)
  14. print("rol don't exist")

huangapple
  • 本文由 发表于 2023年7月23日 23:51:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定