简体   繁体   English

Python - 从 2 个数字中获取唯一的整数

[英]Python - Get an unique integer from 2 numbers

I'm making a game (universe, with 'infinite' amount of stars) with procedural generation and I've faced a little problem getting an unique number from coordinates for seed.我正在制作一个带有程序生成的游戏(宇宙,具有“无限”数量的星星),我在从种子坐标获取唯一数字时遇到了一个小问题。 The world is divided into 16x16 sectors, and each sector has coordinates X and Y. I use python's module random to generate some values by seed (is the sector a star, if so, does it have planets and similar).世界被分成 16x16 个扇区,每个扇区都有坐标 X 和 Y。我使用 python 的模块random通过种子生成一些值(扇区是恒星,如果是,它是否有行星和类似的)。

Also x and y can be negative numbers. x 和 y 也可以是负数。

Here is what I've tried:这是我尝试过的:

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

Use a complex seed (x = real, y = imaginary).使用复数种子(x = 实数,y = 虚数)。

import random as r

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM