gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- User
用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- Assistant
以下是一个基于python的2D柏林噪声实现,具有可传参的frequency、lacunarity、persistence、octaves和seed:
pythonimport numpy as np import random def noise_2d(size, frequency=16, lacunarity=2, persistence=0.5, octaves=1, seed=None): if seed is not None: random.seed(seed) np.random.seed(seed) # generate a grid of points points = np.mgrid[0:size[0], 0:size[1]] amplitude = 1 total = 0 for i in range(octaves): noise = np.random.rand(2, *size) * frequency grid = np.meshgrid( np.linspace(0, 1, size[0], endpoint=False), np.linspace(0, 1, size[1], endpoint=False) ) grid = np.array(grid) grid = (grid - 0.5) * frequency grid = grid.reshape((2, *size, 1)) dot_product = np.sum(grid * noise, axis=0) total += amplitude * dot_product amplitude *= persistence frequency *= lacunarity # normalize value to between 0-255 max_value = np.max(total) min_value = np.min(total) total = (total - min_value) / (max_value - min_value) * 255 return total.astype(np.uint8)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.