Last active
August 29, 2015 14:09
-
-
Save wy36101299/e87c1775c26a52d9b3a0 to your computer and use it in GitHub Desktop.
directional n*n grid produce 有方向性n*n grid 產生器 負號(-)代表被有edge連結
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy as np | |
def proEdge(g,l): | |
for t in l: | |
t = t.split(',') | |
i = int(t[0]) | |
j = int(t[1]) | |
if i > 0: | |
if g[i-1][j]==1: | |
if random.random()>0.5: | |
g[i][j]= -1 | |
else: | |
g[i-1][j]= -1 | |
if i < 3: | |
if g[i+1][j]==1: | |
if random.random()>0.5: | |
g[i][j]= -1 | |
else: | |
g[i+1][j]= -1 | |
if j > 0: | |
if g[i][j-1]==1: | |
if random.random()>0.5: | |
g[i][j]= -1 | |
else: | |
g[i][j-1]= -1 | |
if j < 3: | |
if g[i][j+1]==1: | |
if random.random()>0.5: | |
g[i][j]= -1 | |
else: | |
g[i][j+1]= -1 | |
return g | |
def produce(n): | |
g = np.zeros((n,n),dtype=np.int) | |
rannum = random.randint(4,10) | |
grid = n*n | |
Probability = float(rannum)/float(grid) | |
l=[] | |
for i in range(n): | |
for j in range(n): | |
if Probability>random.random(): | |
g[i][j]=1 | |
l.append(str(i)+','+str(j)) | |
return proEdge(g,l) | |
produce(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment