【问题标题】:random.random() produces the same results in a for looprandom.random() 在 for 循环中产生相同的结果
【发布时间】:2020-01-11 19:40:19
【问题描述】:

我正在制作一个伪词(假的,听起来很真实的词)生成器,它按预期工作,但后来我决定让它一次生成多个单词,(你给它“n”,它给你“n”个单词)。所以我把它全部放在一个 for 循环中......它可以工作......但是比如说你要求 6 个单词,它会给你 6 次同一个单词,而不是每次都生成一个新单词。

import random
wordend = "false"
word = ""
sniptypes = ["v","c"]
vowels = ["a","e","i","o","u"]
vends = ["a","e","y","o","u"]
#vends is short for vowel ends (for when a word ends with a vowel)
cends = ["b","c","d","f","k","l","m","h","n","p","g","q","s","r","t","v","w","x","y","z"]
#cends is short for consonant ends (these are the consonants the words are allowed to end with)
dends = ["kt","rt","ld","rc","rk","fk","lm","by","ch","ck","cy","dy","fy","gh","gy","ky","ly","quy","my","ny","nd","pt","ph","py","ry","rg","rf","rd","xy","sb","sh","sk","sp","st","sy","th","ty","wy","ss","tt","ll","nm","mn"]
#dends is short for double ends (pair of consonants that sound well at the end of a word: chalCK)
consonants = ["b","c","d","f","g","h","j","k","l","m","n","p","qu","r","s","t","v","w","x","y","z"]
doubles = ["bl","br","ch","ck","cl","cr","dl","dr","fl","fn","fr","fy","gh","gl","gr","gn","vy","kl","kn","kr","xy","zy","ly","pn","pt","ph","pl","pr","py","sb","sc","sh","sk","sl","sn","sm","sp","squ","st","sv","sy","th","tl","tr","ty","ts","vl","wh","wr","ll","tt","ss"]
#doubles are a pair of consonants that sound well at the beggining of a word: CHalk.
lengths = [2,3,3,4,4,4,4,5,5,6]
for i in range(int(input("how many words?   "))):
    lenght = random.choice(lengths)
    sniptype = random.choice(sniptypes)
    snipnumb = lenght
    while wordend == "false":
        snipnumb -= 1
        if sniptype == "v":
            r1 = random.random()
            if r1 < (5/21):
                r2 = random.random()
                if r2 < (1/5):
                    word += random.choice(vowels)
                    word += random.choice(vowels)
                    if snipnumb == 0:
                        word += random.choice(vends)
                    elif snipnumb > 0:
                        word += random.choice(vowels)
                    else:
                        print("error")
                elif r2 >= (1/5):
                    word += random.choice(vowels)
                    if snipnumb == 0:
                        word += random.choice(vends)
                    elif snipnumb > 0:
                        word += random.choice(vowels)
                    else:
                        print("error")
                else:
                    print("error")
            elif r1 >= (5/21):
                if snipnumb == 0:
                    word += random.choice(vends)
                elif snipnumb > 0:
                    word += random.choice(vowels)
                else:
                    print("error")
            else:
                print("error")
            sniptype = "c"
        elif sniptype == "c":
            r1 = random.random()
            if r1 < (6/21):
                r2 = random.random()
                if r2 < (1/6):
                    if snipnumb == 0:
                        word += random.choice(dends)
                    elif snipnumb > 0:
                        word += random.choice(consonants)
                        word += random.choice(doubles)
                    else:
                        print("error")
                elif r2 >= (1/6):
                    if snipnumb == 0:
                        word += random.choice(dends)
                    elif snipnumb == (lenght-1):
                        word += random.choice(doubles)
                    elif snipnumb > 0:
                        word += random.choice(consonants)
                        word += random.choice(consonants)
                    else:
                        print("error")
                else:
                    print("error")
            elif r1 >= (6/21):
                if snipnumb == 0:
                    word += random.choice(cends)
                elif snipnumb > 0:
                    word += random.choice(consonants)
                else:
                    print("error")
            else:
                print("error")
            sniptype = "v"
        else:
            print("error")
        if snipnumb == 0:
            wordend = "true"
    print(word)

我希望它生成 n 个不同的单词,相反,它会生成一个单词并打印 n 次。

【问题讨论】:

    标签: python python-3.x random while-loop


    【解决方案1】:

    将生成随机词的代码变成一个函数,然后重复调用该函数。真正相关的更改在底部,但在所有代码中都有一些更改。

    import random
    
    def get_random_word():
      wordend = "false"
      word = ""
      sniptypes = ["v","c"]
      vowels = ["a","e","i","o","u"]
      vends = ["a","e","y","o","u"]
      #vends is short for vowel ends (for when a word ends with a vowel)
      cends = ["b","c","d","f","k","l","m","h","n","p","g","q","s","r","t","v","w","x","y","z"]
      #cends is short for consonant ends (these are the consonants the words are allowed to end with)
      dends = ["kt","rt","ld","rc","rk","fk","lm","by","ch","ck","cy","dy","fy","gh","gy","ky","ly","quy","my","ny","nd","pt","ph","py","ry","rg","rf","rd","xy","sb","sh","sk","sp","st","sy","th","ty","wy","ss","tt","ll","nm","mn"]
      #dends is short for double ends (pair of consonants that sound well at the end of a word: chalCK)
      consonants = ["b","c","d","f","g","h","j","k","l","m","n","p","qu","r","s","t","v","w","x","y","z"]
      doubles = ["bl","br","ch","ck","cl","cr","dl","dr","fl","fn","fr","fy","gh","gl","gr","gn","vy","kl","kn","kr","xy","zy","ly","pn","pt","ph","pl","pr","py","sb","sc","sh","sk","sl","sn","sm","sp","squ","st","sv","sy","th","tl","tr","ty","ts","vl","wh","wr","ll","tt","ss"]
      #doubles are a pair of consonants that sound well at the beggining of a word: CHalk.
      lengths = [2,3,3,4,4,4,4,5,5,6]
      lenght = random.choice(lengths)
      sniptype = random.choice(sniptypes)
      snipnumb = lenght
      while wordend == "false":
        snipnumb -= 1
        if sniptype == "v":
          r1 = random.random()
          if r1 < (5/21):
            r2 = random.random()
            if r2 < (1/5):
              word += random.choice(vowels)
              word += random.choice(vowels)
              if snipnumb == 0:
                word += random.choice(vends)
              elif snipnumb > 0:
                word += random.choice(vowels)
              else:
                print("error")
            elif r2 >= (1/5):
              word += random.choice(vowels)
              if snipnumb == 0:
                word += random.choice(vends)
              elif snipnumb > 0:
                word += random.choice(vowels)
              else:
                print("error")
            else:
              print("error")
          elif r1 >= (5/21):
            if snipnumb == 0:
              word += random.choice(vends)
            elif snipnumb > 0:
              word += random.choice(vowels)
            else:
              print("error")
          else:
            print("error")
          sniptype = "c"
        elif sniptype == "c":
          r1 = random.random()
          if r1 < (6/21):
            r2 = random.random()
            if r2 < (1/6):
              if snipnumb == 0:
                word += random.choice(dends)
              elif snipnumb > 0:
                word += random.choice(consonants)
                word += random.choice(doubles)
              else:
                print("error")
            elif r2 >= (1/6):
              if snipnumb == 0:
                word += random.choice(dends)
              elif snipnumb == (lenght-1):
                word += random.choice(doubles)
              elif snipnumb > 0:
                word += random.choice(consonants)
                word += random.choice(consonants)
              else:
                print("error")
            else:
              print("error")
          elif r1 >= (6/21):
            if snipnumb == 0:
              word += random.choice(cends)
            elif snipnumb > 0:
              word += random.choice(consonants)
            else:
              print("error")
          else:
            print("error")
          sniptype = "v"
        else:
          print("error")
        if snipnumb == 0:
          wordend = "true"
      return word
    
    word_count = int(input("how many words?   "))
    counter = 0;
    while(counter < word_count):
      print(get_random_word())
      counter+=1
    

    【讨论】:

      猜你喜欢
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 2019-04-05
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多