# PSTricks plot random points in polar coordinates

I wish to use PSTricks to plot a random point in a disk of radius 2, following a uniform distribution with respect to polar coordinates (making it less dense far away). I tried out the three following, but PSTricks doesn't understand any of them:

\SpecialCoor
\psdot( Rand(0,2) ; Rand(0,360) )
\psdot( \pscalculate{Rand*2} ; \pscalculate{Rand*360} )
\psdot( ! Rand 2 mul ; ! Rand 360 mul )

• Unfortunately not. I'm sorry, but I don't see how to modify those examples in a way that the density of the scattered points decreases with the distance r. Do you? (As far as I can see all examples involve a uniform distribution in a given area, but I need it uniform with respect the to polar coordinates.) Commented Jun 2, 2020 at 9:15
• To put it in plain math: I would like to scatter the points in polar coordinates ( r ; t ) such that r is random in [0,2] and t is random in [0,360]. (Clearly the result will be a cloud of points in a disk of radius 2, with many points in the middle and less near the booundary.) BTW: Using runit=2 and \degrees=1 my question simplifes to ( r ; t ) such that r and t are both random in [0,1], independently. The problem is that PSTricks does understand (! Rand Rand ) and does not understand neither ( Rand , Rand ) nor ( Rand ; Rand ) Commented Jun 3, 2020 at 10:35

Thank you very much, MOP!! Your solutions works and, as a give-away, it contained the little answer that I was looking for: the command

                             PtoC


that transforms a polar couple to a cartesian couple. If you had only revealed to me those secret four letters, I could have done it all by my own ;-)

Here now comes my little plot program:

\begin{document}
\psset{unit=2cm}
\begin{pspicture}(-2,-2)(2,2)\SpecialCoor
\psframe[framearc=0,linecolor=black,fillstyle=solid,fillcolor=white](-2,-2)(2,2)
\pscircle[fillstyle=solid,fillcolor=lightgray,linewidth=1.1pt](0,0){2}
\psLoop{2500}{\psdot[dotscale=.25](! 2 Rand mul 360 Rand mul PtoC)}
\end{pspicture}
\end{document}

• Modern people never need to invoke \SpecialCoor because it is already enabled by default since 20xx (I forgot). Commented Jun 3, 2020 at 20:18
\documentclass[pstricks,border=5mm]{standalone}
\begin{document}

\psset{unit=4cm}
\begin{pspicture}(1,1)
\psRandom[dotsize=1pt,fillstyle=solid](1,1){\pscircle (0.5,0.5){0.5}}
\end{pspicture}

\begin{pspicture}(1,1)
\psRandom[randInit=42,dotsize=2pt,randomPoints=5000,color,
fillstyle=solid](1,1){\pscircle(0.5,0.5){0.5}}
\end{pspicture}

\end{document}


Consider the following template. The explanation is given after it.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node,pst-plot}
\pstVerb
{
realtime srand
/RMax 5 def
/AMax 360 def
}

\begin{document}
\psLoop{10}{%
\begin{pspicture}(-5,-5)(5,5)
\curvepnodes[plotpoints=100]{0}{360}{RMax Rand mul AMax Rand mul PtoC}{A}
\pscircle{!RMax}
%\foreach \i in {0,1,...,99}{\psline[linecolor=lightgray](0,0)(A\i)}
\foreach \i in {0,1,...,99}{\pscircle*[linecolor=red](A\i){2pt}}
\end{pspicture}}
\end{document}

• \pstVerb{...} : identifiers that are valid only in PostScript context.
• realtime srand : seeding PSTricks' pseudo-random generator.
• RMax : maximum radius
• AMax : maximum angle
• \psLoop{... integer number ...}{... PSTricks objects ...} : looping construct
• \curvepnodes{... start ...}{... stop ...}{... coordinate expression in RPN ...}{... Node ...} : defining nodes on a curve
• Rand : producing a random floating point number between 0 and 1.
• RMax Rand mul : equals to a random floating point between 0 and RMax.
• AMax Rand mul : equals to a random floating point between 0 and AMax.
• r t PtoC : converting polar to cartesian
• ! : RPN prefix for parameter that does not receive RPN by default.

# Extra

If you need to learn how PtoC works, see the following. The new verb MOP converts polar to cartesian as PtoC does.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node,pst-plot}
\pstVerb
{
/MOP {2 copy cos mul 3 1 roll sin mul} bind def
}

\begin{document}
\begin{pspicture}[showgrid=true](-5,-5)(5,5)
\psdots(!5 0 MOP)(!5 90 MOP)
\end{pspicture}
\end{document}


Note that there is no warning if your own PostScript verbs conflict with the existing ones. As a rule of thumb, always use at least 3 upper case letters.