【问题标题】:Python - Get an unique integer from 2 numbersPython - 从 2 个数字中获取一个唯一整数
【发布时间】: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 添加适当的值,使它们从零开始且非负数。
  • XY的域是什么?意味着他们可以有什么价值?还有为什么hash(str()) 在第一次尝试中?
  • 我明白了,你已经提到了。我在问最小值和最大值

标签: python math


【解决方案1】:

使用复数种子(x = 实数,y = 虚数)。

import random as r

x=5.5
y=6.6
sd = x+1j*y
r.seed(sd)
print(r.random())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2022-01-26
    • 2022-01-10
    • 1970-01-01
    • 2010-10-14
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多