Two-input pseudo-random number generator

I need a pseudo-random number generator that gives me a number from the range [-1, 1] (the range is optional) from two inputs of type float.

I will also try to explain why I need this:

I use the Diamond Square algorithm to create a height map for my elevation mechanism. The area is divided into patches (Chunked LOD).

The problem with Diamond-Square is that it uses a random function, so let's say that two adjacent patches share the same point (x, z), then I want the height to be the same for everyone, so I don’t I will get some crack effect.

Some may say that I could get height information from a neighboring patch, but then the result may be different, after which the patch was created first.

So, why do I need a pseudo-random number generator that returns a unique number, given the two inputs that are (x, z).

(I do not ask anyone to write such a function, I just need general feedback and well-known algorithms that do something like this).

+6
source share
3 answers

You need something similar to a hash function for a pair (x, z) .

I would suggest something like

(a * x + b * z + c) ^ d

where all numbers are integers, a and b are large primes, so the integer overflows and c and d are some random integers. ^ bitwise exception or. The result is a random integer that you can scale to the desired range.

This suggests that the map is not used in a game where knowledge of the landscape is essential, since such a function is unsafe to keep it secret. In this case, you better use the cryptographic function.

+4
source

If you are looking for a bijection from IRxIR β†’ [-1; 1], I can suggest the following:

bijection from IR to] -a: a [

First, find the bijection from IR->] -1; 1 [so we just need to find a bijection from IRxIR-> IR

 tan(x): ]-Pi/2;Pi/2[ -> IR arctan(x) : IR -> ]-Pi/2;Pi/2[ 1/Pi*arctan(x) + 1/2: IR -> ]0;1[ 2*arctan(x) : IR->]-Pi:Pi[ 

and

 ln(x) : IR + -> IR exp(x): IR -> R+ 

Bijection from] 0.1 [x] 0.1 [->] 0.1 [

write:

 (x,y) in ]0,1[ x ]0,1[ x= 0,x1x2x3x4...xn...etc where x1x2x3x4...xn represent the decimals of x in base 10 y=0,y1y2y3y4...ym...etc idem Let define z=0,x1y1x2y2xx3y3....xnyn...Oym in ]0,1[ 

Then, by construction, we can prove that this is an exact bijection from] 0,1 [x] 0,1 [to] 0,1 [. (I'm not sure if this is true for the zith number of infinite decimals .. but this is at least a β€œvery good” injection, tell me if I'm wrong)

denote this function: CANTOR (x, y)

then 2 * CANTOR-1 is a bijection of] 0.1 [x] 0.1 [->] -1.1 [

Then combining all of the above statements:

here you go, you get a bijection from IRxIR β†’] -1; 1 [...

You can combine with a bijection from IR->] 0,1 [

 IRxIR -> ]-1;1[ (x,y) -> 2*CANTOR(1/Pi*arctan(x) + 1/2,1/Pi*arctan(y) + 1/2)-1 

let reciproque define, we process the same:

RCANTOR: z β†’ (x, y) (reciproque of CANTOR (x, y)

RCANTOR ((z + 1) / 2):] -1: 1 [->] 01 [x] 0.1 [

 then 1/Pi*tan(RCANTOR((z+1)/2)) + 1/2 : z ->(x,y) ]-1;1[ -> IRxIR 
+2
source

Just select any old hash function, paste in a binary description of the coordinates and use the output.

0
source

Source: https://habr.com/ru/post/891299/


All Articles