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

**URL:** <https://community.wolfram.com/t/generate-random-points-in-a-unit-circle-using-a-for-loop/12585>\
**Category:** Questions\
**Tags:** geometry\
**Created:** [June 10, 2018, 6:38pm UTC](https://community.wolfram.com/t/generate-random-points-in-a-unit-circle-using-a-for-loop/12585 "2018-06-10T18:38:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aajgeeraert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@aajgeeraert](https://community.wolfram.com/u/aajgeeraert)\
**Post date:** [June 10, 2018, 6:38pm UTC](https://community.wolfram.com/t/generate-random-points-in-a-unit-circle-using-a-for-loop/12585/1 "2018-06-10T18:38:05Z")

</div>

I am new to Mathematica and I have a problem with the “For” statement. I am often in doubt where to place a “,” or a “;”.

I am stuck with the following : I want to generate random points in a unit circle with the following function :

```
randompunten[n_] := Module[{p = n, r, angle, lis, i},
  lis = {};
  For[i = 0;
   r = RandomReal[],
   angle = RandomReal[2 \[Pi]],
   i <= p;
   i++;

lis = Append[lis, Point[{r Cos[angle], r Sin[angle]]}]]]; lis]

```

The result is an empty lis

NB : I just found out that this is not the right way but aside of that : what am I doing wrong in the code ?  
(When I do the Append statement outside the module things work fine .

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [June 10, 2018, 6:58pm UTC](https://community.wolfram.com/t/generate-random-points-in-a-unit-circle-using-a-for-loop/12585/2 "2018-06-10T18:58:12Z")

</div>

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](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`.

---

<div class="post-metadata">

**Author:** ![trounev](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/trounev/32/7405_2.png) [@trounev](https://community.wolfram.com/u/trounev)\
**Post date:** [June 12, 2018, 2:52am UTC](https://community.wolfram.com/t/generate-random-points-in-a-unit-circle-using-a-for-loop/12585/3 "2018-06-12T02:52:24Z")

</div>

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

 ![rp.png](https://global.discourse-cdn.com/wolfram/original/3X/5/5/55440bddd9a9923f33bbc89481f5ffc0b132c04e.png)

---

<div class="post-metadata">

**Author:** ![aajgeeraert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@aajgeeraert](https://community.wolfram.com/u/aajgeeraert)\
**Post date:** [June 12, 2018, 7:25am UTC](https://community.wolfram.com/t/generate-random-points-in-a-unit-circle-using-a-for-loop/12585/4 "2018-06-12T07:25:54Z")

</div>

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

Chiel Geeraert

---

<div class="post-metadata">

**Author:** ![aajgeeraert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@aajgeeraert](https://community.wolfram.com/u/aajgeeraert)\
**Post date:** [June 12, 2018, 7:28am UTC](https://community.wolfram.com/t/generate-random-points-in-a-unit-circle-using-a-for-loop/12585/5 "2018-06-12T07:28:12Z")

</div>

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

Chiel Geeraert

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [June 12, 2018, 5:36pm UTC](https://community.wolfram.com/t/generate-random-points-in-a-unit-circle-using-a-for-loop/12585/6 "2018-06-12T17:36:08Z")

</div>

Just for completeness: The function [`RandomPoint`](http://reference.wolfram.com/language/ref/RandomPoint.html) should at least be mentioned.

---

<div class="post-metadata">

**Author:** ![aajgeeraert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@aajgeeraert](https://community.wolfram.com/u/aajgeeraert)\
**Post date:** [June 15, 2018, 11:10am UTC](https://community.wolfram.com/t/generate-random-points-in-a-unit-circle-using-a-for-loop/12585/7 "2018-06-15T11:10:10Z")

</div>

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

Thank you for your reply,  
Chiel Geeraert.
