dimanche 26 avril 2020

a + b mod n vs. a xor b mod n

I'm procedurally generating a World in a Game and therefore use 2 equivalent pseudo-random generators (linear congruential) for x and y axis (giving both a different seed of course).

Now to be able to create different worlds I want to combine both pseudo-random values (e.g. both between 0 and 10) for each position (x,y).

I first thought of hashing (to generally equally hit each number from 0 to 10), but then found that XORing both ints might be more performant (o.c. you might call that hashing too) Now I still want to generate values between 0 and 10. So I would do:

(1) (r1 ^ r2) % 11. ( ^ beeing bitwise XOR)

And here I was wondering if that is equivalent to

(2) (r1 + r2) % 11

And which of both would be more performant. I'd say (1) because it does not carry bits?




Aucun commentaire:

Enregistrer un commentaire