On page 1024 of the PGF manual there is a small code to draw random paths which are "contained", or which don't escape too far from their starting points. I copied part of the code to draw that sort of curve on the background of a picture which already contains random elements; I modified the code by adding the macros \xmax and \ymax to the computation of the coordinates. The result is that this random path always ends very far from the first picture. This result persists whether or not the random path is drawn on the background layer, whatever the parameter in \pgfmathsetseed{<param>}, and whether or not I call the seed function again when drawing it. Here's the code:
\documentclass[tikz]{standalone}
\usetikzlibrary{
calc,
math,
backgrounds,
shapes
}
\pgfmathsetseed%{\number\pdfrandomseed}
{1}
\newcommand\RandNum[1]{random(1,#1)/#1}
\definecolor{almond}{rgb}{0.94, 0.87, 0.8}
\begin{document}
\begin{tikzpicture}[scale = 3.5]
\pgfmathsetmacro{\xmax}{5.5}
\pgfmathsetmacro{\ymax}{3.1}
\pgfmathtruncatemacro{\totfigs}{1.5*(\ymax*\xmax)}
\pgfmathsetmacro{\MaxRand}{\xmax*\ymax}
\foreach ~ in {0,0.2, ..., \totfigs}{
\pgfmathsetmacro{\R}{random(3,12)}
\pgfmathsetmacro{\rsizex}{\RandNum{\MaxRand}}
\pgfmathsetmacro{\rsizey}{\RandNum{\MaxRand}}
\pgfmathsetmacro{\rsize}{\RandNum{\MaxRand}}
\pgfmathsetmacro{\rot}{rnd}
\pgfmathsetmacro{\blau}{rnd}
\pgfmathsetmacro{\grun}{rnd}
\definecolor{coul}{rgb}{\rot,\grun,\blau}
\definecolor{pigm}{cmy}{\rot,\grun,\blau}
\fill[
fill = coul,
opacity = \RandNum{8},
] (rnd*\xmax,rnd*\ymax)%
circle [x radius = \rsizex*0.35, y radius = \rsizey*0.25];
\node[
fill = pigm,
regular polygon,
regular polygon sides = \R,
minimum size = \rsize*2 cm,
opacity = \RandNum{4},
rotate = {random(0,27)}
] at (rnd*\xmax,rnd*\ymax) {};
}
\begin{scope}[on background layer, scale = 0.3, x = 10pt, y = 10pt]
\fill[almond] (current bounding box.south west) rectangle (current bounding box.north east);
\pgfmathsetseed{1}
%%%% THIS PIECE OF CODE: FROM HERE
\coordinate (current point) at (0,0);
\coordinate (old velocity) at (current point);
\coordinate (new velocity) at (rand*\xmax,rand*\ymax);
\foreach ~ in {0,...,100}{
\pgfmathsetmacro{\rot}{rnd}
\pgfmathsetmacro{\blau}{rnd}
\pgfmathsetmacro{\grun}{rnd}
\definecolor{col}{rgb}{\rot,\grun,\blau}
\draw[col, thick]
(current point) .. controls ++([scale=-1]old velocity) and ++(new velocity) ..
++(rnd,rnd) coordinate (current point);
\coordinate (old velocity) at (new velocity);
\coordinate (new velocity) at (rand*\xmax,rand*\ymax);
}
%%%%% UP TO HERE
\end{scope}
\end{tikzpicture}
\end{document}
If I run the same code (the one between the comments in uppercase) on a separate document, it produces a "contained" path, as expected. I attach pictures of both behaviours. However, if I remove the macros \xmax and \ymax from that piece of code and run it, I also obtain a random path that "escapes"; that is, if I compile the following code:
\documentclass[tikz]{standalone}
\usetikzlibrary{calc, math}
\pgfmathsetseed{\number\pdfrandomseed}
\begin{document}
\pgfmathsetmacro{\xmax}{5.5}
\pgfmathsetmacro{\ymax}{3.1}
\begin{tikzpicture}
\coordinate (current point) at (0,0);
\coordinate (old velocity) at (current point);
\coordinate (new velocity) at (rand,rand);
\foreach ~ in {0,...,100}{
\pgfmathsetmacro{\rot}{rnd}
\pgfmathsetmacro{\blau}{rnd}
\pgfmathsetmacro{\grun}{rnd}
\definecolor{col}{rgb}{\rot,\grun,\blau}
\draw[col, thick]
(current point) .. controls ++([scale=-1]old velocity) and ++(new velocity) ..
++(rnd,rnd) coordinate (current point);
\coordinate (old velocity) at (new velocity);
\coordinate (new velocity) at (rand,rand);
}
\end{tikzpicture}
\end{document}
I obtain the third picture. How can I correct this and obtain a path as in the second picture every time? Thanks!





rand"generates a pseudo-random number between -1 and 1 [...]"; the purpose of\xmaxandymaxis to bound the generation of random figures in the first picture of my question. – mathbekunkus May 24 '20 at 21:19rndand notrand. I had not looked carefully. The correct statement is that you wantrnd-0.5. Andrndreally creates a random number between 0 and 1. – May 24 '20 at 21:26rand(third picture, second piece of code on my question) the path still "escapes". – mathbekunkus May 24 '20 at 21:43++(rnd,rnd). – May 24 '20 at 21:45