# How to generate randomly points that belong to the given range?

Consider some rectangle called ExperimentLocation, and a circle ParticleAzimuthalCoverage:

x1 = 0;
y1 = 1;
dx = 2.5;
dy = 2.5;
z1 = 10;
dz = 20;
ROffAxisArb[\[Theta]_, ztodet_] = ztodet*Tan[\[Theta]];
ParticleAsimuthalCoverage[ztodet_, \[Theta]_] :=
Circle[{0, 0}, ROffAxisArb[\[Theta], ztodet]];
experiment = "rectangle"
ExperimentLocation[xtodet_, ytodet_, dxdet_, dydet_, Rdet_,
experiment_] :=
If[experiment == "rectangle",
Rectangle[{xtodet - dxdet/2, ytodet - dydet/2}, {xtodet + dxdet/2,
ytodet + dydet/2}],
If[experiment == "circle", Disk[{xtodet, ytodet}, Rdet], 0]];


In dependence on values $$z,\theta$$, there are different intersections of ExperimentLocation and ParticleAzimuthalCoverage: it may be one region, or two (for a general case there may be more regions). For instance, for a particular values $$z,\theta$$, we have

    Show@Graphics@{RegionBoundary@
ExperimentLocation[x1, y1 + dy/2, dx, dy, Rdet, experiment], Red,
RegionIntersection[
ExperimentLocation[x1, y1 + dy/2, dx, dy, Rdet, experiment],
ParticleAsimuthalCoverage[20.99, 0.168]][[2]]}


My question is how to generate a random number N of azimuthal angle from regions of the intersection (independently of the number of regions)?

• Do you want a random selection from any region or do you want one random selection for each region?
– alex
Mar 23 '21 at 15:30

To give context for the reader, You create a circle and a square and by asking the intersection you generate regions, highlighted in red. The question is whether there is a way to extract an angle randomly, that belongs to any of the segments of your experiment.

regions =
RegionIntersection[
ExperimentLocation[x1, y1 + dy/2, dx, dy, Rdet, experiment],
ParticleAsimuthalCoverage[20.99, 0.168]][[2]];

numOfRegions =
regions //
Length; (* probably a useful variable to have in the near future! \
;) *)

angles = 1. regions[[;; , 3]] (* extract the angle pairs that define a segment*)
randomPoints = {#, RandomReal[#]} &@RandomChoice[angles]


{{1.21201, 1.38713}, {1.75446, 1.92958}}

{{1.21201, 1.38713}, 1.26707}

The last line of the code will randomly select a segment (meaning the two azimuthal angles that define it) and then randomly generate an angle that lies somewhere in between. The only addition I made is so that you can know which segment was chosen as the first element of the list. You could make an association <|segment_1-> {angle1,angle2},....|> but that is a different story.

You can make the minor tweaks that you want from that point onward.

Let me know if I did not quite understand your question.