# How do I combine a Monte Carlo simulation with conditionals?

Here's my problem. I would like to test a whole bunch of random function parameter values for certain conditions, and if they meet all of these conditions, I'd like to add them to a table or an array or something so that I can then plot the points (ignoring the sets of values that do not meet all of these conditions). Here are a few specifics:

My conditions rely on three parametric functions:

0 < (H[m, s][u] /. sol) &&
0 < (S[m, s][u] /. sol) < 4 Pi &&
-4 Pi < (SH[m, s][u] /. sol) < 4 Pi


I want to test random values for m and s across the function domain u = {10^3, 10^18}.

m = RandomInteger[{300, 2200}];
s = RandomReal[{-1, 1}];


I'd like to store (plot) lots of random points that satisfy the conditions, and scrap the rest. What's the best way to do this?

• This might be of some help. – corey979 Sep 8 '16 at 17:34

I'd propose something like the following:

synthetic conditions (note the := here):

conditions := m < 1000 && -0.5 < s < 0.5 && PrimeQ[u]


and a Do loop:

out = {};
Do[SeedRandom[];
m = RandomInteger[{300, 2200}];
s = RandomReal[{-1, 1}];
If[conditions, {AppendTo[out, {m, s, u}], Continue[]},
Continue[]], {u, 10^3, 10^4}
]


Part of the output:

{{556, 0.321089, 1009}, {575, 0.345256, 1013}, {600, -0.0361481, 1103}, {522, 0.0317783, 1277}, {746, -0.40744, 1321}, {325, -0.187146, 1367}, {447, 0.130137, 1483}, {360, -0.284384, 1487}, {357, 0.0764024, 1489}, {661, 0.473819, 1571}, {713, 0.389343, 1601}, {373, -0.00995126, 1607}, {867, -0.448495, 1619},...}

• Thanks for the lead! However, I need the points {m,s} which satisfy the entire domain given for u. That's the tricky part in my opinion. I can't just test random values as you've done. I need the conditions to be satisfied over the entire domain. – Ash Arsenault Sep 8 '16 at 18:24
• I don't really get it. You explicitly wrote "test random values for m and s", and "m = RandomInteger[{300, 2200}]; s = RandomReal[{-1, 1}];'". You can of course change the range of u from {u, 10^3, 10^4} in my exaple to what you desire. – corey979 Sep 8 '16 at 18:32
• Am I incorrect in understanding that with the code you've provided, only one value of 'u' is tested with each m-s pair? – Ash Arsenault Sep 8 '16 at 18:34
• Yes. How many do you want? E.g., m` should take all integer values from 300 to 2200? – corey979 Sep 8 '16 at 18:35
• 'm' and 's' can be random. I don't need every value for those -- that's fine. It's just that for every random pair of 'm' and 's' values, the conditions must be satisfied over all 'u' in my domain. Does that make sense? Sorry, I'm really struggling to express my needs. Haha. – Ash Arsenault Sep 8 '16 at 18:38