1

I need a method where I can provide a Vector2 location and a radius, which will give me a random point in the radius, with higher chance towards the center than the edge of the radius.

I have tried to search for a solution for this problem but have not been able to decribe the problem well enough in searchengines to come up with a good result.

Hope someone here can point me in the right direction. A code example in any language is highly appreciated!

Severin
  • 962
  • 5
  • 21

2 Answers2

2

How about this:

  1. Generate three random numbers with uniform distribution in the range [-R, R]. What you get is (x,y,z) describing a point drawn randomly (with uniform distribution) in a cube.
  2. If x2+y2+z2 > R2, reject the point and start over on step one. (More than half the points you generate will pass this test.)
  3. What you have now is a point drawn randomly (with uniform distribution) in the interior of a sphere of radius R.
  4. Now discard z, and you are left with (x,y) describing a point within a radius R of the center, more likely nearer the center than farther away because a sphere is thicker in the middle than at the edges.
Beta
  • 96,650
  • 16
  • 149
  • 150
1

Not sure if that's exactly what you want, but it should give you some distribution with more points per volume in the center of the sphere:

You can use this simple formula to find a random evenly distributed point on a spheres surfaces: Generate a random sample of points distributed on the surface of a unit sphere

When you take this point and move it inwards by multiplying it with a random value and dividing by r:

(x,y,z) *= randomValue

where (x,y,z) is the vector pointing at the random point on the surface and randomValue is some random value between 0 and 1.

This gives an even distribution with respect to r, but since the surface are gets smaller with smaller radius, your points will concentrate in the middle.

If you want to further tweak the distribution, you can add some power to randomValue:

(x,y,z) *= pow(randomValue, exponent)

where the higher the exponent, the more points you will get in the middle of the sphere.

QuantumDeveloper
  • 737
  • 6
  • 15
  • Thank you for the suggestion. I was actually looking for a Vector2 solution. I have updated the original question. I am applying it to a Vector3 in the end, but the Y value remains untouched. My apologies. – Severin May 02 '20 at 17:46
  • 1
    You can still apply this method to a circle. Just ignore the angle phi and the z coordinate, and you are done. – QuantumDeveloper May 02 '20 at 18:19