【发布时间】:2020-10-25 09:50:05
【问题描述】:
我正在制作一个带有程序生成的游戏(宇宙,具有“无限”数量的星星),我在从种子坐标中获取唯一数字时遇到了一个小问题。
世界分为16x16个扇区,每个扇区都有坐标X和Y。我使用python的模块random通过种子生成一些值(扇区是恒星吗,如果是,是否有行星之类的)。
x 和 y 也可以是负数。
这是我尝试过的:
random.seed(hash(str((x << 32) + y)))
#works but very slow
random.seed((x & 0xFFFF) << 16 | (y & 0xFFFF))
#also works but if coordinate is bigger than 16 numbers, it just teleports to the center
random.seed(x*y)
#doesn't work, because coordinates can be x10,y15 and x15,y10, so the number is not unique
random.seed(x * (y << 16))
#the world gets mirrored by half
【问题讨论】:
-
您想要两个坐标中的一个种子编号?
16*X+Y怎么样(对于从零开始的坐标)。 -
我的想法和你的第一个类似:
random.seed(hash((x, y))) -
您可以为 x 和 y 添加适当的值,使它们从零开始且非负数。
-
X和Y的域是什么?意味着他们可以有什么价值?还有为什么
hash(str())在第一次尝试中? -
我明白了,你已经提到了。我在问最小值和最大值