1
$\begingroup$

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:

enter image description here

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

enter image description here

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?

$\endgroup$
3
  • 3
    $\begingroup$ Add a SeedRandom call with an appropriate seed to get identical output from RandomReal every time you run it. $\endgroup$
    – MarcoB
    Jun 22, 2016 at 13:35
  • $\begingroup$ 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 ? $\endgroup$
    – S.S.
    Jun 22, 2016 at 13:52
  • $\begingroup$ @MarcoB have you an idea how to use SeedRandom in my example ? $\endgroup$
    – S.S.
    Jun 22, 2016 at 13:58

2 Answers 2

3
$\begingroup$

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]
]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ That's interesting @MarcoB .. thanx $\endgroup$
    – S.S.
    Jun 22, 2016 at 23:19
3
$\begingroup$

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

enter image description here

$\endgroup$
5
  • $\begingroup$ Hi @Kuba, it's pleasant to know my point is not reachable .. $\endgroup$
    – S.S.
    Jun 22, 2016 at 13:56
  • $\begingroup$ @ 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 ..` $\endgroup$
    – S.S.
    Jun 22, 2016 at 14:07
  • $\begingroup$ @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. $\endgroup$
    – Kuba
    Jun 22, 2016 at 14:24
  • $\begingroup$ @ Kuba . I mean I try to use that Module in plotting for my example, if you have an idea.. $\endgroup$
    – S.S.
    Jun 22, 2016 at 16:01
  • $\begingroup$ @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. $\endgroup$
    – Kuba
    Jun 22, 2016 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.