生成随机数

生成随机数

我有一个问题。我有一张图,我必须在该图的每个顶点粘贴一个随机数。顶点有坐标。

部分代码:

\foreach \pos/\name in {{(0,0)/a}, {(1,-1)/b}, {(1.5,-2)/c},
                                {(1.3,-2.5)/d}, {(0.6,-2.5)/e}, {(2,-2.66)/f}, 
                                {(1.3,-3.5)/g},{(0.6,-3.5)/h},{(2,-3.33)/i},
                                {(1.9,-1.3)/j}}
            \node[circle,fill=black!25,minimum size=15pt,inner sep=0pt] (\name) at \pos {\pgfmathsetmacro{rand}};

答案1

这是一个解决方案:

\documentclass[tikz]{standalone}
\pgfmathsetseed{\pdfuniformdeviate 10000000} % new seed at each compilation
\begin{document}
\begin{tikzpicture}
  \foreach \pos/\name in {
    {(0,0)/a},
    {(1,-1)/b},
    {(1.5,-2)/c},
    {(1.3,-2.5)/d},
    {(0.6,-2.5)/e},
    {(2,-2.66)/f}, 
    {(1.3,-3.5)/g},
    {(0.6,-3.5)/h},
    {(2,-3.33)/i},
    {(1.9,-1.3)/j}%
  } {
    \pgfmathsetmacro\myvalue{abs(rand)}
    \pgfset{number format/.cd,fixed}
    \node[circle,fill=black!25,minimum size=15pt,inner sep=0pt,font=\tiny]
    (\name) at \pos {\pgfmathprintnumber{\myvalue}};
  }
\end{tikzpicture}
\end{document}

相关内容