Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.6K Views
|
6 Replies
|
4 Total Likes
View groups...
Share
Share this post:

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

Posted 7 years ago

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 .

POSTED BY: Chiel Geeraert
6 Replies
Posted 7 years ago

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

Thank you for your reply, Chiel Geeraert.

POSTED BY: Chiel Geeraert

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

POSTED BY: Henrik Schachner
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 7 years ago

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

Chiel Geeraert

POSTED BY: Chiel Geeraert
POSTED BY: Szabolcs Horvát
Posted 7 years ago

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

Chiel Geeraert

POSTED BY: Chiel Geeraert
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard