how to generate random number in a rectangle like this?
6 views (last 30 days)
William Rose on 2 Nov 2022
@Daniel Niu, I will assume that there are 5 equally likely options, and that for each option, you want to pick a random point in the gray region.
Then you can do it with two random steps:
1. Pick a random integrer in [1,5]. The tells you which of the 5 cases you are currently using.
2. Pick a random 2D point in the gray region selected by step 1.
When doing step 2, you may want to do a random coin flip if you are in case 2 or 3, because the gray region in case 2 and case 3 has two non-contiguous squares. Alternatively, if you are in case 2 or case 3, you can pick a pont in the "superset" rectangle of case 2 or 3, then reject the point and try again if the point you got is not in one of the two gray squares that are active for that case.