I'm not sure what exactly is the problem you are having. This is exactly how Voronoi diagrams look like
Note that you are (to as much as I understand the code) initializing points to
x:= rand mod nx;
y:= rand mod ny;
with
rand being a random number from 0 to 254 and
nx=640, ny=360 ->
x and
y will always be within 0..254 range, i.e. your points crowd to top-left corner of the image, which is exactly what you observe in the image and
I guess this is the part you are calling "error" and "failure" (otherwise please specify what exactly doesn't work for you). Everything beyond this range is just a "tail" of those points, exactly as it should be.
e.g. replacing those for
x:= Round(Random * nx);
y:= Round(Random * ny);
And not forgetting
Randomize before calling
Random results in attached image.
Do not rush into optimizations. First make sure you understand the code well and/or the code does exactly what you need it to do. Optimizations most often "reduce the ability to read the code", which is exactly the opposite of what you need now.