2016-12-03 105 views
1

我最近一直在尋找使程序生成的遊戲地形。我看到佩林噪音對此很有用,所以我給了它一個鏡頭。到目前爲止,地形生成得很漂亮。但是,無論何時我多次運行該程序,地形都完全相同。有沒有任何方法來隨機產生的柏林噪音?柏林噪音的Python隨機種子

代碼:

from opensimplex import OpenSimplex 
import random 
from time import time 

height = 40 
width = height 
scale = height/10 

value = [[0 for x in range(width)] for y in range(height)] 

gen = OpenSimplex() 
def noise(nx, ny): 
    # Rescale from -1.0:+1.0 to 0.0:1.0 
    return gen.noise2d(nx, ny)/2.0 + 0.5 

def printBiome(y, x): 
    if value[y][x] <= 2: 
    print('O', end = " ") 
    elif value[y][x] >= 8: 
    print('M', end = " ") 
    else: 
    print('L', end = " ") 

for y in range(height): 
    for x in range(width): 
     nx = x/width - 0.5 
     ny = y/height - 0.5 
     value[y][x] = 10 * noise(1 * scale * nx, 1 * scale * ny) + 0.5 * noise(2 * scale * nx, 2 * scale* ny) + 0.25 * noise(4 * scale * nx, 4 * scale * ny) 

for y in range(height): 
    for x in range(width): 
     printBiome(y, x) 
    print() 

回答

1

的OpenSimplex類defaults to using seed=0。要生成不同的地形,請輸入不同的種子值:

import uuid 
# http://stackoverflow.com/a/3530326/190597 
seed = uuid.uuid1().int>>64 
gen = OpenSimplex(seed=seed)