繁体   English   中英

每次在while循环中,如何使随机生成的数字不同?

[英]How can I have the randomly generated number be different each time in the whileloop?

我面临的问题是,在进行用户广播时,值中随机生成的数字应该是随机的。 但是,循环开始后,所有值始终保持不变


hp = 1000
Fireball = random.randint(5, 10)
Iceblast = random.randint(0, 20)
Healingtouch = random.randint(5, 10)

MiniBug = 100
bspell1 = random.randint(0, 5)
bspell2 = random.randint(10, 20)
bspell3 = 15
bheal1 = 10
bossturn = random.choice([bspell3, bspell2, bspell1, bheal1])

while MiniBug >= 0:
    usercasting = input("Cast a Spell: ")
    if usercasting == "Fireball":
        print("Your spell did ", Fireball, "damage to the enemy!")
        MiniBug -= Fireball
        print("MiniBug has", MiniBug, "Hp left!")

    if usercasting == "Iceblast":
        MiniBug -= Iceblast
        print("Your spell did ", Iceblast, "damage to the enemy!")
        print("MiniBug has", MiniBug, "Hp left!")

    if usercasting == "Healingtouch":
        hp += Healingtouch
        print("You healed yourself by", Healingtouch, "!")
        print(hp)

    if MiniBug != 0:
        if bossturn == bspell1:
            hp -= bspell1
            print("The enemy attacked you for", bspell1, ",and your current hp is", hp)
        if bossturn == bspell2:
            hp -= bspell2
            print("The enemy attacked you for", bspell2, "and your current hp is", hp)
        if bossturn == bspell3:
            hp -= bspell3
            print("The enemy attacked you for", bspell3, ", and your current hp is", hp)
        if bossturn == bheal1:
            MiniBug += bheal1
            print("The enemy healed himself by", bheal1, "and his hp is", MiniBug)

我正在寻找的结果是在每次“用户广播”之后以及当bossturn时都有随机生成的数字。

编辑:在重读问题时,我想我误会了。

如果您每次运行代码,一次又一次使用相同的数字

random.randint(lowerbound,upperbound)返回一个数字,从下限到上限,但仅在调用时返回。 因此,在while语句内,您不会在while语句内生成其他随机数。 这意味着一旦设置,bspell1,bspell2和bossturn都将具有不变的值。

您需要将bossturn = random.choice([bspell3,bspell2,bspell1,bheal1])移到while语句中,如下所示:

hp = 1000


MiniBug = 100

bspell3 = 15
bheal1 = 10

while MiniBug >= 0:
    // some of these need to be moved into their appropriate if statements, but to generate
    // new values each round, they need to be in the while loop
    bossturn = random.choice([bspell3, bspell2, bspell1, bheal1])
    Fireball = random.randint(5, 10)
    Iceblast = random.randint(0, 20)
    Healingtouch = random.randint(5, 10)
    bspell1 = random.randint(0, 5)
    bspell2 = random.randint(10, 20)
    usercasting = input("Cast a Spell: ")
    if usercasting == "Fireball":
        print("Your spell did ", Fireball, "damage to the enemy!")
        MiniBug -= Fireball
        print("MiniBug has", MiniBug, "Hp left!")

    if usercasting == "Iceblast":
        MiniBug -= Iceblast
        print("Your spell did ", Iceblast, "damage to the enemy!")
        print("MiniBug has", MiniBug, "Hp left!")

    if usercasting == "Healingtouch":
        hp += Healingtouch
        print("You healed yourself by", Healingtouch, "!")
        print(hp)

    if MiniBug != 0:
        if bossturn == bspell1:
            hp -= bspell1
            print("The enemy attacked you for", bspell1, ",and your current hp is", hp)
        if bossturn == bspell2:
            hp -= bspell2
            print("The enemy attacked you for", bspell2, "and your current hp is", hp)
        if bossturn == bspell3:
            hp -= bspell3
            print("The enemy attacked you for", bspell3, ", and your current hp is", hp)
        if bossturn == bheal1:
            MiniBug += bheal1
            print("The enemy healed himself by", bheal1, "and his hp is", MiniBug)

如果每次运行代码,它都会生成相同的数字序列

您可能遇到一个问题,其中每次运行时随机数生成器的种子都相同。 您可以通过根据当前时间设置种子来避免这种情况。

import random
import time
random.seed(time.clock())

这样可以确保伪随机的第一个随机数应该与所选集合的随机概率相当接近。

这样做的原因是CS中的随机数并不是真正的“随机”,而是部分根据传入的种子值确定。通过这种方式,您可以将相同的种子值传递给随机数生成器,并获得相同的序列一次又一次地收集数字(如果您想复制使用大量随机数的东西,例如在游戏《矮人要塞》中,如果需要,可以使用种子再次创建“相同”世界)。

对于非重复的“随机”数问题,有三种通用的解决方案:

  • 如果您想从较大范围中获取一些数字,请选择一个,如果重复则拒绝。 如果范围较大,则不会造成太多重复尝试。

  • 如果您希望从一个较小的范围获得大量数字,请在数组中列出所有数字,然后对数组进行随机排列。 从改组后的数组中依次获取随机数。

  • 如果您希望从较大范围获得大量数字,请使用适当大小的加密算法。 例如,对于64位数字,请使用DES并依次加密0、1、2、3,...。 由于加密是可逆的,因此保证了输出的唯一性。 查看非标准大小的格式保留加密

正如@Valentino所说,您没有生成真正的随机数。 原始RNG始终可以产生重复的数字。

暂无
暂无

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

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