Xtrouble835
Xtrouble835

Reputation: 11

random.random() produces the same results in a for loop

I'm making a pseudo-word (fake, real-sounding word) generator, it was working as expected, but then I decided to make it so it can generate more than one word at a time, (you give it "n" and it gives you "n" number of words). So I put it all in a for loop... and it works... but say for example you ask for 6 words, it gives you the same word 6 times instead of generating a new word each time.

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)

I expect it to make n different words, instead, it makes one word and prints it n times.

Upvotes: 1

Views: 85

Answers (1)

Matthew Gaiser
Matthew Gaiser

Reputation: 4803

Make the code which generates the random word a function and just repeatedly call the function. The really relevant change is at the bottom, but there are a few changes throughout all the code.

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

Upvotes: 1

Related Questions