# Selecting and plotting points belonging to a region from a random sample

I'm facing a problem with RandomReal when using ListPlot to plot some constrained points. I found that at every run of MA kernel the graph's points changes. In details, suppose for instance, functions:

k1[s_, d_] := 3 s + 5 d

k2[s_, d_] := 5 s - 7 d


I'm using the following to plot conditional k1 and k2 in (s,d) plan

ClearAll[ps]

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

styleps =
Style[{##}, PointSize[.01],
Piecewise[{{Blue, 0 < k1[#, #2] <= 1 && 0 < k2[#, #2] <= 2}},
White]] & @@@ ps;

ListPlot[styleps, 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 for the first run this gives:

While if I make ClearAll[ps] and rerun or quit the kernel and started again, this gives totally different points, such as:

This is expected from RandomReal because it generates random points each time, but this is totally confusing in this case , because how one can determine all real points which satisfy the required condition if in each run only some points are appear ?

So any suggestions to improve this code to can plot whole points of of s versus d for which k1 and k2 conditions are satisfied for any run?

• Add a SeedRandom call with an appropriate seed to get identical output from RandomReal every time you run it. Jun 22, 2016 at 13:35
• The problem is that even I fixed RandomReal for each run, it won't generate whole required points for this fix but it will be upon my choice such as setting length of genertaed points, etc. Can I avoid RandomReal from the beginning and generate points in more efficient way ?
– S.S.
Jun 22, 2016 at 13:52
• @MarcoB have you an idea how to use SeedRandom in my example ?
– S.S.
Jun 22, 2016 at 13:58

Here is an approach that generates many more points than you had, selects the ones for which your conditions are met, and plots them:

k1[s_, d_] := 3 s + 5 d
k2[s_, d_] := 5 s - 7 d
ps = RandomVariate[UniformDistribution[{{0.1, 2}, {-1, 3}}], 1000000];

valid = Select[ps, 0 < k1[Sequence @@ #] <= 1 && 0 < k2[Sequence @@ #] <= 2 &];

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


• That's interesting @MarcoB .. thanx
– S.S.
Jun 22, 2016 at 23:19

Your approach is a dead end, you can't determine all points by picking them and checking conditions. Because there are infinitely many of them.

ImplicitRegion[
0 < k1[x, y] <= 1 && 0 < k2[x, y] <= 2,
{x, y}
] // RegionPlot


• Hi @Kuba, it's pleasant to know my point is not reachable ..
– S.S.
Jun 22, 2016 at 13:56
• @ Kuba I still have a question, how can we imply set length as you mentioned condition[x_] := .1 < x < .5; Module[{i = 0}, First@Last@ Reap@While[i < 5, If[condition[#], i++; Sow[#]] &@RandomReal[]]] to my example ? Also do you think SeedRandom mentioned above can do the same act with RandomReal ? sorry I wish if I'm an expert in MA ..
– S.S.
Jun 22, 2016 at 14:07
• @S.S. I don't understand, the cardinality of set of points fulfilling your conditions is the cardinality of the continuum. The problem isn't about MMA skills I think. p.s. SeedRandom and RandomReal are related but they do different things, press F1 and read more.
– Kuba
Jun 22, 2016 at 14:24
• @ Kuba . I mean I try to use that Module in plotting for my example, if you have an idea..
– S.S.
Jun 22, 2016 at 16:01
• @S.S. replace i with number of points you want to enerate, RandomReal[1, {2}]` or something. I really don't know what is the goal, you can't get that way all the points.
– Kuba
Jun 22, 2016 at 17:01