Search code examples
pythonloopsrandom

How to constantly generate random numbers inside a loop and apply the result to a variable?


I am trying to make a 2D game using Ursina. All has gone well until I generated terrain via a loop. It works well, but when I try to make the types of chunks random using random.randint, it only generates a number once.

 import random
 class chunk_type():
      chunk_type=random.randint(1,2)
 world_size=8
 for i in range(world_size):
    chunk_type()
    print(chunktype.chunktype)

The above code should output 8 random numbers between 1 and 2. I have tried updating methods and loops. The radiant method only executes the code once so when I printed the variable for debugging it printed all a string of only one number. There are no error messages. How do I fix this?


Solution

  • If your only looking for integers you can do this.

    import random
    
    def chunk_type():
          return random.randint(1,2)
    
    world_size=8
    
    for i in range(world_size):
        random_int = chunk_type()
        print(random_int)