Simple Voronoi Code not working

I tried to convert a fast voronoi code on this page, I tried so many different combinations of code and there is always the same error, can anyone see what is wrong with my code? it looks like the loop is generating different values for adjacent tiles. its supposed to cycle through 3x3 sets of tiles.

here’s original code:

``````float voronoi( in vec2 x )
{
ivec2 p = floor( x );
vec2  f = fract( x );

float res = 8.0;
for( int j=-1; j<=1; j++ )
for( int i=-1; i<=1; i++ )
{
ivec2 b = ivec2( i, j );
vec2  r = vec2( b ) - f + random2f( p + b );
float d = dot( r, r );

res = min( res, d );
}
return sqrt( res );
}
``````

and unity version that doesnt work:

``````function rndng ( n: float ): float
{//random number -1, 1
var e = ( n *321.9)%1;
return  (e*e*111.0)%2-1;
}

function voronoi(  vtx: Vector3  )
{
var px = Mathf.Floor( vtx.x );
var pz = Mathf.Floor( vtx.z );
var fx = Mathf.Abs(vtx.x%1);
var fz = Mathf.Abs(vtx.z%1);

var res = 8.0;
for( var j=-1; j<=1; j++ )
for( var i=-1; i<=1; i++ )
{
var rx = i - fx + rndng(px*pz + i );
var rz = j - fz + rndng(px*pz + j );
var d = Vector2.Dot(Vector2(rx,rz),Vector2(rx,rz));
res = Mathf.Min( res, d );
}
return Mathf.Sqrt( res );
}
``````

``````	var rx = i - fx + nz2d(px+i ,pz + j ) ;