Home > Blockchain >  ValueError with random class in python not working
ValueError with random class in python not working

Time:11-05

Here is the code:

import random
import numpy as np

class Room:
  def __init__(self, name, contents):
    self.name = name
    self.contents = contents

rooms = np.zeros((11, 11))
maxRooms = 7
possibleNextRoom = []

def resetLevel():
  global rooms

  for r in range(len(rooms[0])):
    for c in range(len(rooms[1])):
      rooms[r][c] = 0

  possibleNextRoom = []

halfHeight = int(len(rooms[1]) / 2)
halfWidth = int(len(rooms[0]) / 2)
rooms[halfWidth][halfHeight] = 1

def countRooms():
  global rooms

  roomCount = 0

  for r in range(len(rooms)):
    for c in range(len(rooms)):
      if rooms[r][c] == 1:
        roomCount  = 1

  return roomCount

def findPossibleRooms():

  for r in range(len(rooms) - 1):
    for c in range(len(rooms) - 1):
      if rooms[r][c] == 1:
        if rooms[r][c 1] != 1:
          possibleNextRoom.append((r, c 1))
        if rooms[r][c-1] != 1:
          possibleNextRoom.append((r, c-1))
        if rooms[r-1][c] != 1:
          possibleNextRoom.append((r-1, c))
        if rooms[r 1][c] != 1:
          possibleNextRoom.append((r 1, c))

def addRoom():

  nextRoom = random.randrange(0, len(possibleNextRoom))
  rooms[possibleNextRoom[nextRoom][0]][possibleNextRoom[nextRoom][1]] = 1
  possibleNextRoom.pop(nextRoom)

def generateLevel():
  resetLevel()

  while countRooms() < maxRooms:
    countRooms()
    findPossibleRooms()
    addRoom()

def displayLevel():
  print(rooms)

generateLevel()
displayLevel()

Here is the Error:

ValueError: empty range for randrange() (0, 0, 0)

I thought I was using random correctly, but appearantly not. I tried making the array start with something in it, but it still gave me the same error. I have been working on this for a long time and its giving me a headache. Any help with this is greatly appreciated. The error is on line 54.

CodePudding user response:

In [90]: import random
In [91]: random.randrange?
Signature: random.randrange(start, stop=None, step=1, _int=<class 'int'>)
Docstring:
Choose a random item from range(start, stop[, step]).
...

This produces your error:

In [92]: random.randrange(0,0)
Traceback (most recent call last):
  File "<ipython-input-92-566d68980a89>", line 1, in <module>
    random.randrange(0,0)
  File "/usr/lib/python3.8/random.py", line 226, in randrange
    raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0, 0, 0)

So if the error occurs in (telling us the line number if 52 is nearly useless)

nextRoom = random.randrange(0, len(possibleNextRoom))

it means possibleNextRoom is an empty list, [] (or possibly something else with a 0 len).

I see a

possibleNextRoom = []

The append in findPossibleRooms operate in-place, so can modify this global list. But they are wrapped in for and if, so I can't say whether they run at all.

In any case, the error tells that nothing has been appended, for one reason or other.

  • Related