How to create 10 random x, y coordinates in a grid

2019-06-27 12:02发布

I need to create a 8x8 grid and distribute 10 coins in random positions on the grid. The problem I am facing is that the randint function will sometimes generate the same random co-ordinates and therefore only 9 or 8 coins are generated and placed on the grid. How can I make sure this doesn't happen? Cheers :) This is my code so far:

from random import randint

grid = []
#Create a 8x8 grid
for row in range(8):
    grid.append([])
    for col in range(8):
        grid[row].append("0")

#create 10 random treasure chests
    #problem is that it might generate the same co-ordinates and therefore not enough coins
for coins in range(10):
    c_x = randint(0, len(grid)-1)
    c_y = randint(0, len(grid[0])-1)
    while c_x == 7 and c_y == 0:
           c_x = randint(0, len(grid)-1)
           c_y = randint(0, len(grid[0])-1)
    else:
        grid[c_x][c_y] = "C"

for row in grid:
print(" ".join(row))

I have included a while/else - as there must not be a coin in the bottom left corner of the grid

4条回答
Luminary・发光体
2楼-- · 2019-06-27 12:33

You could add another check in your while loop to make sure there is not already a coin at the currently chosen coordinate. BTW, you could also avoid the checks you already have by changing the range of your randint directly to match your needs.

Or you could generate all possible 7*7=49 coordinates (eliminating the unwanted coordinates) and then pick 10 different at random using the np.random.choice function.

查看更多
forever°为你锁心
3楼-- · 2019-06-27 12:48

So you wish to generate 10 random unique coordinates?

You can use a set to verify:

cords_set = set()
while len(cords_set) < 10:
    x, y = 7, 0
    while (x, y) == (7, 0): 
        x, y = randint(0, len(grid) - 1), randint(0, len(grid[0]) - 1)
    # that will make sure we don't add (7, 0) to cords_set
    cords_set.add((x, y))

This will generate a set of tuples that represent (x, y) coordinates.

A few examples of the output of print(cords_set):

{(5, 6), (7, 6), (4, 4), (6, 3), (7, 4), (6, 2), (3, 6), (0, 4), (1, 7), (5, 2)}

{(7, 3), (1, 3), (2, 6), (5, 5), (4, 6), (3, 0), (0, 7), (2, 0), (4, 1), (6, 5)}

{(1, 2), (1, 3), (6, 7), (3, 3), (4, 5), (4, 4), (6, 0), (1, 0), (2, 5), (2, 4)}
查看更多
女痞
4楼-- · 2019-06-27 12:50

You only have 64 cases, so you can generate all coordinates as tuples (x,y) and then you can use random.sample to directly have 10 unique elements, so you don't have to check or redraw.

import random
from itertools import product

g = [['0' for _ in range(8)] for _ in range(8)]

coord = list(product(range(8), range(8)))
for coins in random.sample(coord, 10):
    g[ coins[0] ][ coins[1] ] = 'C'

for row in g:
    print(' '.join(row))
查看更多
相关推荐>>
5楼-- · 2019-06-27 12:52

Look at the code below:

from random import randint

grid = []
#Create a 8x8 grid
for row in range(8):
    grid.append([])
    for col in range(8):
        grid[row].append("0")

for coins in range(10):
    c_x = randint(0, len(grid)-1)
    c_y = randint(0, len(grid[0])-1)
    while grid[c_x][c_y] == "C":
        c_x = randint(0, len(grid) - 1)
        c_y = randint(0, len(grid[0]) - 1)
    grid[c_x][c_y] = "C"

The after generating the coordinates you check to make sure there is no 'C' in place before assigning one to it. If there is you draw again and re-check. If there is not, you assign one and draw the next.

Let me know if this helps ☺

查看更多
登录 后发表回答