I want to be able to reproduce identical heatmaps using the diamond square algorithm (http://ift.tt/1ZSr5E5). I set the seed at the beginning, but even so, the algorithm produces a different heatmap everytime. They area only slightly different, but not identical. Here is my MWE:
import random
myrandom=random.Random(1)
def diamondSquareGrid(self,grid,width,height,myrandom,roughness):
#initialize the 4 corner randomly:
rands=[]
r=myrandom.randrange(-100,100)
rands.append(r)
grid[0][0]=r
r = myrandom.randrange(-100, 100)
rands.append(r)
grid[0][width-1]
r = myrandom.randrange(-100, 100)
rands.append(r)
grid[height-1][0]
r = myrandom.randrange(-100, 100)
rands.append(r)
grid[height-1][width-1]
n=floor(log(self.grid.height)/log(2))
for i in range(n):
startwidth=pow(2,0)-1
endwidth=pow(2,n-i)
startheight=pow(2,0)-1
endheight=pow(2,n-i)
for j in range(pow(2,2*i)):
midheight=int((endheight+startheight)/2)
midwidth=int((endwidth+startwidth)/2)
r = myrandom.random()
rands.append(r)
grid[startheight][midwidth]=int((grid[startheight][startwidth]+
grid[startheigth][endwidth])
/2+roughness*(2*r-1))
r = myrandom.random()
rands.append(r)
grid[midheight][startwidth]=int((grid[startheight][startwidth]+
grid[endheigth][startwidth])
/2+roughness*(2*r-1))
r = myrandom.random()
rands.append(r)
grid[endheight][midwidth]=int((grid[endheight][endwidth]+
grid[endheigth][startwidth])
/2+roughness*(2*r-1))
r = myrandom.random()
rands.append(r)
grid[midheight][endwidth]=int((grid[endheight][endwidth]+
grid[startheigth][endwidth])
/2+roughness*(2*r-1))
r = myrandom.random()
rands.append(r)
grid[midheight][midwidth]=int((grid[startheight][startwidth]+
grid[startheigth][endwidth]+
grid[endheight][startwidth]+
grid[endheight][endwidth])
/2+roughness*(2*r-1))
if endwidth+pow(2,n-i)>=self.grid.width:
startwidth=0
endwidth=floor(pow(2,n-i))
startheight=endheight
endheight=endheight+floor(pow(2,n-i))
else:
startwidth=endwidth
endwidth=endwidth+floor(pow(2,n-i))
return rands
As you can see I tracked the random variates themselves. When I run the function multiple times with the same seed the rands
list contains the same numbers. This makes me think that when it calculates the new values from the old ones there is a rounding error which propagates and accumulates over all the iterations (I even casted all of the values to int, because I do not particularly care about that, but with no luck).
If that is the case, where exactly is the error introduced and how can I get around this to be able to reproduce the heatmap exactly from the random seed? Thanks for any help!
Aucun commentaire:
Enregistrer un commentaire