# Why this code gives different output for differents runs

Edit

Here is my code to plot many constrained variables in (x,y) plan :

the variables are :

   k1[x_,y_]:= 1/(2v^2 Cos[x]^2)(Sin[y]^2 m2^2+Cos[y]^2 m1^2)

k2[x_,y_]:= 1/(2v^2 Sin[x]^2)(Cos[y]^2 m2^2+Sin[y]^2 m1^2)


withv = 178 Sqrt2; m1 = 125; m2 = 750;

Now to make a plot satisfies conditions on k1 and k2 in ArcTan(x) and ArcTan(y) plan I used the following code:

ClearAll[ps]

ps = Transpose[{RandomReal[{0.1, 2.}, 1000], RandomReal[{-1, 3}, 1000]}];

styleps3 =
Style[{##}, PointSize[.01],
Piecewise[{{Blue,
0 < k1[ArcTan[#], ArcTan[#2]] <= 4 Pi &&
0 < Sin[ArcTan[#] - ArcTan[#2]] <= 0.01 &&
0 < k2[ArcTan[#], ArcTan[#2]] <= 4 Pi}}, White]] & @@@ ps;

ListPlot[styleps3, DataRange -> {{-1, 1}, {-1, 1}}, Frame -> True,
GridLines -> {Table[i, {i, 0, 2, 0.1}], Table[i, {i, -1, 3, 0.2}]},
ImageSize -> 500, Axes -> False, GridLinesStyle -> Lighter[Gray]]


Now the problem is that as RandomReal is used in this code, only some points are generated which satisfy the required conditions, while there may be still other points satisfy the conditions and has not been generated .. this can be clear when the ListPlot graph gives different points at each run (as plots below)

So any suggestions to improve this code or make alternative to can plot whole points of of tan y versus tan x satisfy required conditions for any run ?

Or:

• Maybe I've missed something but aren't you evaluating RandomReal each time?
– Kuba
Commented Jun 22, 2016 at 8:14
• Yap, I make ClearAll[ps], and retry or quit the kernal at all then begin again ..
– S.S.
Commented Jun 22, 2016 at 8:20
• So why are you expecting it to be the same?
– Kuba
Commented Jun 22, 2016 at 8:22
• Oh, that's right .. but how I get the exact points of tan(y) versus tan tan(x) which meet all conditions .. I mean now I can't specify , for one run it gives two points, while for other it gives 5 points ?
– S.S.
Commented Jun 22, 2016 at 8:30
• Can you give detailed example @Kuba ?
– S.S.
Commented Jun 22, 2016 at 8:52

Here is an approach similar to the one I proposed in your more recent question.

Due to the way you had constructed your conditions, this time I found it more expedient to use Pick instead of Select. Pick required the generation of an intermediate selector list, but this step doesn't really slow down computations too much; in the end, the time-to-plot seems dominated by ListPlot when the number of points is large enough.

Clear[k1, k2]
v = 178 Sqrt@2; m1 = 125; m2 = 750;
k1[x_, y_] := 1/(2 v^2 Cos[x]^2) (Sin[y]^2 m2^2 + Cos[y]^2 m1^2)
k2[x_, y_] := 1/(2 v^2 Sin[x]^2) (Cos[y]^2 m2^2 + Sin[y]^2 m1^2)

ps = RandomVariate[UniformDistribution[{{0.1, 2}, {-1, 3}}], 1*^6];

selector =
0 < k1[ArcTan[#1], ArcTan[#2]] <= 4 Pi &&
0 < Sin[ArcTan[#1] - ArcTan[#2]] <= 0.01 &&
0 < k2[ArcTan[#1], ArcTan[#2]] <= 4 Pi & @@@ ps;

ptsToPlot = Pick[ps, selector];

ListPlot[
ptsToPlot,
DataRange -> {{-1, 1}, {-1, 1}},
Frame -> True, Axes -> False,
GridLines -> {Table[i, {i, 0, 2, 0.1}], Table[i, {i, -1, 3, 0.2}]},
GridLinesStyle -> Lighter[Gray], ImageSize -> 500
]


Although this approach can be made to work, at least to some extent, ultimately a better result would be obtained with less effort using RegionPlot and ImplicitRegion, as Kuba has suggested in his answer to your second question.

• @ MarcoB . You are great .. deeply thanks :)
– S.S.
Commented Jun 23, 2016 at 4:18
• @ MarcoB . Can this code little bit modified to include loop on a third parameter , i.e. if we have k1[x_, y_,z_] instead and z takes values say from 2 to 10, with all the same conditions on k1 and k2 ?
– S.S.
Commented Jun 23, 2016 at 4:22
• @S.S. How would this third parameter be involved in the conditions? Commented Jun 23, 2016 at 4:50
• @ MarcoB. Hi , just add to k1, as selector = 0 < k1[ArcTan[#1], ArcTan[#2],z] <= 4 Pi && 0 < Sin[ArcTan[#1] - ArcTan[#2]] <= 0.01 && 0 < k2[ArcTan[#1], ArcTan[#2]] <= 4 Pi & @@@ ps;
– S.S.
Commented Jun 23, 2016 at 6:33
• "Update" . Hi @ MarcoB , may be you find that for this case ListPlot won't depend at all on z variable, cause we plot so rare region with strong boundry 0 < Sin[ArcTan[#1] - ArcTan[#2]] <= 0.01 .. but you may look on my second post: mathematica.stackexchange.com/questions/119139/…… . you will find that ListPlot changes for different values of m, so Looping over m` values can make a sense
– S.S.
Commented Jun 23, 2016 at 10:08