1
$\begingroup$

I want to make two List/Table(B,C) to separate my values from an existing table(A). To illustrate my question, there is my code:

xyrandom[n_] := Table[{RandomReal[{0, 1}], RandomReal[{0, 1}]}, {i, n}];
xyrandomgra = xyrandom[5000];

From this point, I want to do that if the value x^2+y^2 < 1, take the x,y value and put it in another table(B) else put the value and put it in a third table(C). I have tried :

j = 1;
For[i = 1, i <= 5000,
 If[xyrandomgra[i, 1]^2 + xyrandomgra[i, 2]^2 < 1, {intgraph[j, 1] = xyrandomgra[i, 1], intgraph[j, 2] = xyrandomgra[i, 2] , j++}]; i++]

v = 1;
For[w = 1, w <= 5000,
 If[xyrandomgra[w, 1]^2 + xyrandomgra[w, 2]^2 < 1, (extgraph[v, 1] = xyrandomgra[w, 1] && extgraph[v, 2] = xyrandomgra[w, 2] && v++)]; w++]

But I feel I have to "establish" my 2 new tables (intgraph, extgraph) but I wonder how I would do it since I don't know in advance how many input I will have in both tables. What I have done is how I would do it in C or another language but I don't know in Mathematica.

Thanks you very much to enlight me on this subject,

Patrick

$\endgroup$
1
  • 1
    $\begingroup$ fyi: RandomReal[{0,1}, {n, 2}] $\endgroup$
    – Kuba
    Jan 26, 2017 at 21:00

1 Answer 1

9
$\begingroup$
n = 10^4;

data = RandomReal[{0, 1}, {n, 2}];

split = GatherBy[data, #.# >= 1 &];

ListPlot[split, AspectRatio -> Automatic, Epilog -> Circle[]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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