I currently have a 2d array generated by this code:
JavaScript
x
5
1
for y in range(width):
2
self.grid.append([])
3
for x in range(width):
4
self.grid[y].append(Cell(x, y))
5
If I want to select a random cell, how would I go about it? random.choice doesn’t seem to be working.
Advertisement
Answer
You could do something like
JavaScript
1
4
1
yCoord = random.randrange(width)
2
xCoord = random.randrange(width)
3
randomCell = self.grid[yCoord][xCoord]
4
If you do want to use random.choice
, you’ll have to use it twice, as the first call will return an array of Cells, then the second one will return a cell element.