Severin
Severin

Reputation: 1075

Method that returns random point in radius of provided Vector2, with higher chance at centre of radius than edge

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!

Upvotes: 1

Views: 128

Answers (2)

Beta
Beta

Reputation: 99172

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.

Upvotes: 2

QuantumDeveloper
QuantumDeveloper

Reputation: 777

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.

Upvotes: 1

Related Questions