user13650002
user13650002

Reputation:

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).

Also x and y can be negative numbers.

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

Upvotes: 0

Views: 258

Answers (1)

LevB
LevB

Reputation: 953

Use a complex seed (x = real, y = imaginary).

import random as r

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

Upvotes: 1

Related Questions