Message Boards Message Boards

Generate random points in a unit circle using a "For" loop?

GROUPS:
POSTED BY: Chiel Geeraert
Answer
7 days ago

The , (not the ;) separates arguments, and For needs to have precisely 4 arguments.

If you are a beginner, I suggest you simply forget that For even exists, and use other constructs instead. For is never necessary, and it is very, very rarely the best option. See here for more details:

https://mathematica.stackexchange.com/questions/134609/why-should-i-avoid-the-for-loop-in-mathematica

This post discusses precisely the type of problem you are trying to solve, so be sure to read through all of it. You can do the same with a shorter and faster Table.

POSTED BY: Szabolcs Horvát
Answer
7 days ago

Just for completeness: The function RandomPoint should at least be mentioned.

POSTED BY: Henrik Schachner
Answer
5 days ago
randompunten[n_] := Module[{p = n, r, angle, lis, i}, lis = {};
  For[i = 0, i < p, 
   i++, {r = RandomReal[], angle = RandomReal[2 \[Pi]],
    lis = Append[lis, Point[{r Cos[angle], r Sin[angle]}]]}]; lis]
{Graphics[{Red, randompunten[5], Blue, Circle[{0, 0}, 1]}], 
 Graphics[{Red, randompunten[100], Blue, Circle[{0, 0}, 1]}]}

fig 1

POSTED BY: Alexander Trounev
Answer
6 days ago

Thank you for your reply. I tried with the Do command and indeed no problem there.

Chiel Geeraert

POSTED BY: Chiel Geeraert
Answer
5 days ago

This really clears thing up for me. Thank you (and mr Horvat) very much for your comments.

Chiel Geeraert

POSTED BY: Chiel Geeraert
Answer
5 days ago

Even more simple ! Alas, function not available in my version.

Thank you for your reply, Chiel Geeraert.

POSTED BY: Chiel Geeraert
Answer
2 days ago

Group Abstract Group Abstract