3

I have been trying to generate a graph where two vertices are connected with probability p. Here is what I tried so far,

\pgfmathsetseed{\number\pdfrandomseed}
\begin{tikzpicture}
 % size of circle
 \def \radius {2cm}
 \def \margin {8}
 % number of vertices
 \def \n {5}
 % Probability for an edge to show up between vertices
 \def \p {0.5}

 % Drawing the vertices/nodes
 \foreach \s in {1,...,\n}
  \node[draw, circle] (\s) at ({360/\n * (\s - 1)}:\radius) {};

 % Drawing the edges
 \foreach \s in {1,...,\n}
  \foreach \t in {\s,...,\n}
    % Generate and store a random number to the variable \dummynum
    \pgfmathparse{rnd}
    \pgfmathsetmacro{\dummynum}{\pgfmathresult}
    % If dummynum is less than p, edge is drawn; otherwise nothing happens
    \ifthenelse{\lengthtest{\dummynum pt < \p pt}}{\draw (\t) -- (\s);}{}
\end{tikzpicture}

The compiler has been unsuccessful in compiling it, giving me an error "Paragraph ended before \pgffor@next was complete".

I am sure that the problem is in the ifthenelse line, but so far I have no clue how to fix it. Can anyone point out the error and how to fix it? Thanks.

1 Answer 1

5

Rather than deal with the \ifthenelse macro from the xifthen package, I prefer to use \ifdim ...\fi (which the tikz parser recognizes). There is also the \pgfmathifthenelse, but that would require using "..." which my editor automatically converts to ``...'' (one of its less useful features). Also, \foreach requires the code executed to be inside a {...} group. Finally, I have no idea what you were trying to do with \pgfmathsetseed.

\documentclass[]{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
 % size of circle
 \def \radius {2cm}
 \def \margin {8}
 % number of vertices
 \def \n {5}
 % Probability for an edge to show up between vertices
 \def \p {0.5}

 % Drawing the vertices/nodes
 \foreach \s in {1,...,\n}
  \node[draw, circle] (\s) at ({360/\n * (\s - 1)}:\radius) {};

 % Drawing the edges
 \foreach \s in {1,...,\n}{
  \foreach \t in {\s,...,\n} {
    % Generate and store a random number to the variable \dummynum
    \pgfmathparse{rnd}
    %\let\dummynum=\pgfmathresult
    % If dummynum is less than p, edge is drawn; otherwise nothing happens
    \ifdim\pgfmathresult pt < \p pt\relax \draw (\t) -- (\s);\fi
  }}
\end{tikzpicture}

\end{document}
3
  • I guess I misunderstood the use of pgfmathsetseed - but thanks for the working example!
    – Pig
    Mar 9, 2016 at 5:57
  • @user31415 you can use pgfmathsetseed to always have the same graph from one compilation to the next. Just call \pgfmathsetseed{31415} at the beginning, and change the value if you don't like the way your random graph looks.
    – tarulen
    Mar 9, 2016 at 9:29
  • PDF seed changes much faster than PGF as it takes the minute. That's why that command is used.
    – percusse
    Mar 9, 2016 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.