1

I am trying to draw lines with a different, random length but the same direction. This is what I thought should work, but if I put in any other angle than 90 degrees (for example 30 degrees in this example) the lines don't point in the same direction.

\documentclass{scrartcl}
\usepackage[papersize={5.5cm,8cm}, left=0.5cm,right=0.5cm,top=1cm,bottom=1cm,margin=0pt]{geometry}
\pagestyle{empty}
\usepackage{tikz}
\usepackage{pgfmath}

\begin{document}
\centering
\begin{tikzpicture}[remember picture,overlay]
\foreach \x in {0,1,...,\paperwidth}{
\draw(current page.south west)++(\x pt,0)--++(30:rnd);
}
\end{tikzpicture}
\end{document}

1 Answer 1

2

Use the \pgfmathparse and \pgfmathresult for getting a random number.

\documentclass{scrartcl}
\usepackage[papersize={5.5cm,8cm}, left=0.5cm,right=0.5cm,top=1cm,bottom=1cm,margin=0pt]{geometry}
\pagestyle{empty}
\usepackage{tikz}
\usepackage{pgf}

\pgfmathsetseed{\number\pdfrandomseed} % Getting different random numbers. If you don't want, comment this.

\begin{document}
\centering

\begin{tikzpicture}[remember picture,overlay]
\foreach \x in {0,1,...,\paperwidth}{
\pgfmathparse{int(rand*10)}\let\A=\pgfmathresult
\draw(current page.south west)++(\x pt,0)--++(80:\A);
}
\end{tikzpicture}
\end{document}
1
  • That does it but could you perhaps explain a bit why you are doing that? It seems that TikZ transforms the coordinate into something like ({rnd*cos(80)},{rnd*sin(80)}) and assigns the random number only afterwards. You evade this by first computing the random number and then constructing the path.
    – user121799
    Feb 20, 2019 at 18:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .