I'm trying to define random variables - for example to draw some random points of the form (r,r) when r is a random number. I tried \def\myrandomnumber{rnd}, but that seems to give me a new random number every time. What's to be done?

  • Have you tried specifying the seed: \pgfmathsetseed{} Jan 23, 2012 at 21:36
  • I did, it doesn't help. I guess every time I call "myrandomnumber", it is replaced with "rnd" and generates a different random number.
    – Freddy
    Jan 23, 2012 at 21:39
  • If you want to use the same random multiple times in the same run, the @Jake's solution should work. If you want to obtain the same "random" results each time you run then you should use \pgfmathsetseed{}. Jan 23, 2012 at 22:03

2 Answers 2


Here is an example of using \pgfmathsetseed{} to ensure that subsequent runs yield identical results with a random number. I also make use of Jake's solution using \pgfmathsetmacro to store the result so that it can be used more than once.

Depending on when you run this, the blue pictures will vary (since \pgfmathsetseed{} was not used), but the red ones should not change. Since each of the red images are identical it is obvious that the random numbers did not change between runs, but note that each of the three blue pictures are different.

enter image description here



    \draw [ultra thick, out=120, in=60,#1] (\Xa,\Ya) to (\Xb,\Yb);
    \draw [ultra thick, out=230, in=19,#1] (\Xa,\Ya) to (\Xb,\Yb);



You should save the result of the rnd operator to a macro. The easiest way to do that is to say \pgfmathsetmacro<\macroname>{rnd}. Then you can use the random number where you need it (like \draw (0,0) -- (<\macroname>,<\macroname>);), and draw a new one by repeating the \pgfmathsetmacro... command.

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.