玩矩阵

时间:2016-04-27 20:50:01

标签: python loops matrix random generator

我试图编写一个创建零矩阵的Python类,然后使用随机数生成器来选择矩阵上的点。它将该点中的零点更改为1,直到矩阵为全1。有人可以批评/更正我的代码吗? (我还希望生成器检查它在矩阵上的接近程度,并尝试3次以找到距离任何一个点2个点的位置。)

import random
import numpy as np
#agents is amount of agents available to fill grid
class Gridmodel():
    def __init__(self, gridsize, agents):
        self.gridsize = gridsize
        self.agents = agents 
        self.gridmodel = np.zeros([self.gridsize, self.gridsize],dtype=int)


    def foundspot(self):
        foundspot = False         
        tries = 0
        while foundspot == False and tries <= 3:
            x = random.randint(0, self.gridsize)
            y = random.randint(0, self.gridsize)
            if self.gridmodel[x][y] < 0:
                foundspot = True
        else: 
            tries += 1

    def goodspot(self, x, y):
        goodspot = self.gridmodel[x][y]
        for i in range(-1,2):
            for j in range(-1,2):

                print i, j, self.gridmodel[i][j]             

0 个答案:

没有答案