Compute a single random value p between 0 and (width * height - rect_width * rect_height)Sorry; completely misinterpreted the question the first time.
If p is less than width * top_border, then x = p % width and y = p / width, and we're done.
Otherwise subract width * top_border from p. Now, if p is less than (width - rect_width) * rect_height, then x = p % (width - rect_width) and y = top_border + p / (width - rect_width); then if x >= left_border then add rect_widthyou just want to pick a point randomly on the xedge of a rectangle, and we're done.try something like this:
Otherwise, subtract (width - rect_width) * rect_height from p, and set x = p % width and y = bottom_border + p / width, and you're done.
p = random.randint(0, width + width + height + height) if p < (width + height): if p < width: obj.pos.x = p obj.pos.y = 0 else: obj.pos.x = width obj.pos.y = p - width else: p = p - (width + height) if p < width: obj.pos.x = width - p obj.pos.y = height else: obj.pos.x = 0 obj.pos.y = height - (p - width) BasicallySimply, without the cut-out you can calculateit picks a single variable p between 0 and width * height and set x = p % width and y = p / width. It's very easy to show that that's uniform. From that you have to subtractrandom point on a line the size ofsame length as the cut-out (so you can see howperimeter of the distribution should stay uniform); andrectangle, then wrap this new rangepiecewise wraps that line around the cut-out in three sections. Above, beside,rectangle to give x and belowy coordinates.
The 'beside' case is tricky, because every x result needs to appear on one side orTo ensure that the other ofdistribution stays uniform at the cut-outcorners, and never insideeach of it. This is done by starting with a value in the rangefour segments are taken as inclusive-exclusive intervals [0, width-rect_width)(can be 0, cannot be width or height) and for any valuethey're placed such that is greater than left_border moving it right to appear on the other sideeach one of that rectangle.
I might come back and explain this better, and maybe draw some pictures, but more likely I'll forget which is why I started writing this when I don't have much time to do it wellthem starts in a different corner.