# How to convert current 'state' of machine after having produced pdf using rand-containing coordinates into deterministic TikZ code reproducing it?

Question. In TikZ, if using coordinates of the form (x,y)+(0.5*rand,0.5*rand), is it possible to recover the coordinates that were used to produce the pdf in the very last compilation one did?

Remarks.

• This seems to be a difficult problem. In principle, it should be possible to do this (the information is there), though it seems that one will have to have deep knowledge of the internals of how TikZ works. If I am not overlooking something easy (which I might very well be doing), then this is about somehow mapping a state of the finite-state-machine-that-is-the-machine-you-are-working-with to an explicit compilable TikZ code.
• An example of what is meant by question is this: suppose you have written TikZ code, using randomized coordinates, using the 'rand' function, to simulate random geometric graphs, and suppose you wish to preserve a specific 'run' of your code. ('Preserve' in the sense of 'create explicit deterministic TikZ code which will ever recreate this particular pdf.) You are now facing the following situation: you have the pdf (which you could save, of course), yet (practically) no future run of your present code will reproduce this particular pdf. What can one do to get from this very state of the machine one is working with to an explicit TikZ code recreating this pdf?

• I am aware that TikZ allows one to interact with the 'seeds' of the pseudo-random-number-generator used, and I am aware that the manual speaks about this. Yet I could not make this into a practical solution. The question is really: suppose you are staring at the 'run' that you would like to preserve (in the above sense). In principle, the information should be recoverable, but how?

• Useful related threads are this and this, yet I could not use them to solve this problem.

The current random seed is stored in \pgfmath@rnd@z (see How to get the random seed used in a tikzpicture?). You can set the seed using \pgfmathsetseed. So, for a given run, you can display the seed (in the pdf or on the console and log file) and, if you like it, you can remember the value and set it for a new run.

Example (network code from How can I randomly perturb the position of a grid of nodes?):

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
% pseudo-random seed value, uncomment to create new random graph
%\pgfmathsetseed{\number\pdfrandomseed}
% fixed seed value, use to reproduce graphs
\pgfmathsetseed{856811429}
\tikzset{
small-node/.style={
shape=circle,
fill=white,
draw,
minimum size=+4mm}
}
\begin{document}
\begin{tikzpicture}
% define macro to retrieve current seed
\makeatletter
\def\pgfcurrentseed{%
\pgfmathparse{\pgfmath@rnd@z}\pgfmathresult%
}
\makeatother
% call the macro
\pgfcurrentseed
% print in console and in log file
\typeout{Current seed: \pgfmathresult}
% print in pdf
%\node (a) at (7,0) {Current seed is \pgfcurrentseed};
\foreach \i in {0,...,4}
\foreach \j in {0,...,4}
\node [small-node] (n-\i\j) at (\i + 0.5*rand,\j + 0.5*rand) {};
\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);
\end{tikzpicture}
\end{document}


pdf:

Console and log file (near the end):

Current seed: 856811429
[1] (./randomgraph.aux) )
(see the transcript file for additional information)
Output written on randomgraph.pdf (1 page, 4538 bytes).