Finding a random point on a 2D plane split into multiple sections

So I have a 2D area in which I want to find a random point. The way I would usually do this is by getting the minimum and maximum values for the X and Z coordinates (the ‘ends’) and picking a random number in the range. I already have functions for doing this. However, this time, my 2D area is split into multiple parts that may or may not be connected to one another, as opposed to just a single rectangle. I had two ideas:

  1. Pick a random point within a big rectangle that encompasses the whole area, then check if it’s actually in the area. This is cheaty and hacky, and with a maximum number of tries of 5 it was only successful about half of the time.
  2. Pick a random part, then pick a point within that part. The issue with this is that it doesn’t account for some parts being larger than others, meaning there is a bias. Fixing this would arguably be just as hacky as the above.

Does anyone know of a more elegant solution?

Find a random point on each part, then randomly select which point to use by assigning weights to each point proportional to the size of the part.

1 Like