簡體   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