How can I make random placed edges between two graph partitions?

I have to nodes (V1) and (V2) (representing sets of graph vertices) and I want to draw, say, 10 edges(lines) between them. I don't want to hardcode random locations so I did:

\begin{pgfonlayer}{bg} % so they don't overlap the graphic representation of V1 and V2
\draw (V1)+(rand*.3,rand*.3) -- (V2)+(rand*.3,rand*.3);
\draw (V1)+(rand*.3,rand*.3) -- (V2)+(rand*.3,rand*.3);
\draw (V1)+(rand*.3,rand*.3) -- (V2)+(rand*.3,rand*.3);
%   ... repeat more 7 times ... (PS:I know tikz has a repeat/foreach/... command -
\end{pgfonlayer}

copying and pasting was just for being lazy)

There are two problems with this approach:

1. (Main one) Lines are starting at random locations within V1 but they always end on the center if V2 --- as if rnd was returning 0 in those cases.
2. (Not that important) When using \pause, the value are randomized again on the next slide which is not desired in my case. I realize I should store somehow those random values so that does not happen. But personally, if doing this makes the solution much harder I prefer not to solve it.
3. (OK, bonus one) Is there a way to change the style of those edges (ie, turn them to dashed) on the next slide (ie, after a pause). (Apparently, \draw[\alt{2}{...}{...}] doesn't work..
• The lines are supposed to overlay the nodes or not? – Claudio Fiandrino Aug 28 '13 at 17:11
• They are not, but that's already solved since I putted them on a deeper pgflayer. I'm adding this to the question to make it clearer thanks! – Anonymous31415 Aug 28 '13 at 17:15
• It is no surprise that they always end on V2, after all it is the point you specified after --, the following +(…) specifies a new Move-To operation (as does the one after (V1)). Use ([shift={(rnd*.3,rnd*.3)}] V2) or calc instead. — For a discussion about TikZ in combination with beamer overlays, see How to make beamer overlays with Tikz node – Qrrbrbirlbel Aug 28 '13 at 17:32

A possible solution:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}

\begin{document}

\begin{frame}
\begin{tikzpicture}
\node[circle,draw,fill=white](V1)at (0,0){V$_1$};
\node[circle,draw,fill=white](V2)at (2,2){V$_2$};
\pgfmathsetseed{12345}
\foreach \step/\style in {1/solid,2/,3/dashed,4/solid,5/solid,6/loosely dotted,7/dashdotted,8/solid,9/dashdotted,10/dashdotted}{
\begin{pgfonlayer}{background}
\draw<\step->[\style] (V1)+(rnd*.3,rnd*.3) -- ($(V2)+(rnd*.3,rnd*.3)$);
\end{pgfonlayer}
}
\end{tikzpicture}
\end{frame}

\end{document}

The result:

Main points:

1. to use \pgfmathsetseed to keep the same seed in generating the random numbers: this let the edges not change between sequent overlay;
2. to use a loop setting the overlay and the style of the edge;
3. to exploit the calc library (see page 134 of the pgfmanual for more details) to compute the final point, otherwise it will be the center of the node V2 (as the coordinates are no longer updated).

Version 2

As Qrrbrbirlbel was suggesting, by providing the styles of the edge, the variable \step it is not more needed.

The new code is:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}

\begin{document}

\begin{frame}
\begin{tikzpicture}
\node[circle,draw,fill=white](V1)at (0,0){V$_1$};
\node[circle,draw,fill=white](V2)at (2,2){V$_2$};
\pgfmathsetseed{12345}
\foreach \style in {solid,,dashed,solid,solid,loosely dotted,dashdotted,solid,dashdotted,dashdotted}{
\begin{pgfonlayer}{background}
\draw<+->[\style] (V1)+(rnd*.3,rnd*.3) -- ($(V2)+(rnd*.3,rnd*.3)$);
\end{pgfonlayer}
}
\end{tikzpicture}
\end{frame}

\end{document}

Notice: this solution is equivalent to the previous one. It also allows to not specify the style in the loop and automatically the default solid style will apply.

• OK, but I still don't understand why declaration on nodes V1 and V2 on the path are non simetrical (why you inserted $between V2's coords?) – Anonymous31415 Aug 28 '13 at 17:22 • @Anonymous31415: I've accomplished also point 3 as well as inserted more explanation about the mehod. – Claudio Fiandrino Aug 28 '13 at 17:29 • Excellent! I wish only I could understand the lack of symmetry on such a symmetric example. – Anonymous31415 Aug 28 '13 at 17:33 • Untested, but I think you can remove \step and just use <+->. – Qrrbrbirlbel Aug 28 '13 at 17:35 • @Qrrbrbirlbel: you're right. That is a remaining from the previous version, but it's not fundamental as one provides the edge styles. – Claudio Fiandrino Aug 28 '13 at 17:50 Besides the things that already have been said, here is a calc solution that mimics the border-finding process of -- and randomizes the nodes’ angular anchors where the lines start and end. Of course, depending on shape and size of the nodes and the amplitude of the rand function, the lines may protrude into the nodes again. Code \documentclass{beamer} \usepackage{lmodern,tikz} \usetikzlibrary{calc} \tikzset{ rand --/.style={ to path={ let \p{aux@direc}=($(\tikztotarget)-(\tikztostart)$), \n{aux@direc}={atan2(\p{aux@direc})} in (\tikztostart.\n{aux@direc}+rand*#1) -- (\tikztotarget.180+\n{aux@direc}+rand*#1) \tikztonodes } } } \begin{document} \begin{frame} \begin{tikzpicture}\pgfmathsetseed{130537} \node[circle,draw] (V1) at (0,0) {V$_1$}; \node[circle,draw] (V2) at (2,2) {V$_2$}; \foreach \cnt in {1,...,10} \draw<+-> (V1) to[rand --=10] (V2); \tikzset{xshift=2cm} \node[circle,draw] (V1) at (0,0) {V$_1$}; \node[circle,draw] (V2) at (2,2) {V$_2$}; \foreach \cnt in {1,...,10} \draw<+-> (V1) to[rand --=50] (V2); \tikzset{xshift=2cm} \node[circle,draw] (V1) at (0,0) {V$_1$}; \node[circle,draw] (V2) at (2,2) {V$_2$}; \foreach \cnt in {1,...,10} \draw<+-> (V1) to[rand --=90] (V2); \tikzset{xshift=2cm} \node[draw] (V1) at (0,0) {V$_1$}; \node[draw] (V2) at (2,2) {V$_2\$};
\foreach \cnt in {1,...,10} \draw<+-> (V1) to[rand --=50] (V2);
\end{tikzpicture}
\end{frame}
\end{document}