I have a grid of nodes generated by the following code:

        minimum size=+4mm}
\foreach \i in {0,...,4}
    \foreach \j in {0,...,4}
        \node [small-node] (n-\i\j) at (\i,\j) {};
\foreach \i in {0,...,4}
    \foreach \j [count=\jj] in {0,...,3}
        \draw (n-\i\j) -- (n-\i\jj) (n-\j\i) -- (n-\jj\i);

Is there a way that as each node is created, I can generate two random numbers in the range (-0.5,0.5) and add those two the x and y coordinates of the node so that they are no longer in a rectilinear grid pattern?


You can use the rnd or rand pgf functions. rnd generates a pseudo-random number between 0 and 1 with a uniform distribution, and rand does the same between -1 and 1.

Simply change the node declaration line with the following:

\node [small-node] (n-\i\j) at (\i + 0.5*rand,\j + 0.5*rand) {};


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.