# Plotting a random function f(x) and f(x)+sin(x) with tikz

I'm plotting a function f(x) with random values as follows:

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\draw[color=blue, samples=150]  plot[domain=0:10] (\x,rand) node[below] {$f(x)$};
\end{tikzpicture}
\end{document}


Now I want to plot a new Function g(x) which is defined as g(x)=f(x)+sin(x). Is there a way to do that?

I would really appreciate any help.

Thanks

Yes, this is easily possible if you set the seed directly before the plot and use the same samples and domain settings.

If you don’t want to set the seed manually, you can store it before the first plot and re-set it afterwards.

## Code

\documentclass[tikz,convert=false]{standalone}
\makeatletter
\def\pgfmathstoreseed#1{\let#1\pgfmath@rnd@z}
\let\pgfmathrestoreseed\pgfmathsetseed
\makeatother
\begin{document}
\begin{tikzpicture}
\pgfmathstoreseed\mySeed
\draw[color=blue, samples=250] plot[domain=0:10] (\x,rand/10) node[right] {$f(x)$};
\pgfmathrestoreseed\mySeed
\draw[color=red,  samples=250] plot[domain=0:10] ({\x,rand/10 + sin (\x r)})
node[below] {$f(x) + \sin x$};
\end{tikzpicture}

\begin{tikzpicture}
\pgfmathsetseed{126838}
\draw[color=blue, samples=250] plot[domain=0:10] (\x,rand/10) node[right] {$f(x)$};
\pgfmathsetseed{126838}
\draw[color=red,  samples=250] plot[domain=0:10] ({\x,rand/10 + sin (\x r)})
node[below] {$f(x) + \sin x$};
\end{tikzpicture}
\end{document}
\end{document}


## Output

• That is exactly what I was looking for. Thank you. Commented Aug 5, 2013 at 18:41