【问题标题】:How to randomize the seed of the "noise" library如何随机化“噪声”库的种子
【发布时间】:2019-03-16 01:59:41
【问题描述】:

我想用 Perlin Noise 创建一个 2D 浮点列表。我希望每次运行程序时生成的值都不同。但是,我不确定如何为我在 GitHub here 上找到的噪声库提供随机种子。

如何让程序每次运行时生成不同的值?

我的代码:

from __future__ import division
import noise
import math
from singleton import ST


def create_map_list():
    """
    This creates a 2D list of floats using the noise library. It then assigns
    ST.map_list to the list created. The range of the floats inside the list
    is [0, 1].
    """

    # used to normalize noise to [0, 1]
    min_val = -math.sqrt(2) / 2
    max_val = abs(min_val)

    map_list = []

    for y in range(0, ST.MAP_HEIGHT):
        row = []

        for x in range(0, ST.MAP_WIDTH):
            nx = x / ST.MAP_WIDTH - 0.5
            ny = y / ST.MAP_HEIGHT - 0.5
            row.append((noise.pnoise2(nx, ny, 8) - min_val) / (max_val - min_val))

        map_list.append(row )

    ST.map_list = map_list

【问题讨论】:

    标签: python python-2.7 perlin-noise noise-generator


    【解决方案1】:

    噪声库不支持种子。在实际状态下,你不能有随机输出。

    但是,已经发布了一个pull request 来解决这个问题。

    为此,您必须在获得修改后的代码后重新构建库。 (python setup.py install)

    【讨论】:

    • 谢谢你,你能回答这么老的问题真是太好了。
    【解决方案2】:

    一个简单的方法是在噪声函数中将一个随机数加到xy,这样你也可以使用random.seed()。我正在开发一个 Minecraft 世界生成项目,并且正在使用噪音。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 2021-10-23
      • 2022-08-03
      • 1970-01-01
      • 2017-02-01
      • 2011-06-20
      • 2017-01-20
      相关资源
      最近更新 更多