2

I am trying to create a random walk (as a substitute for a true Brownian motion) which starts and ends at 0.

I generate the random walk using random numbers for each step, and the standard way of making it return to 0 at the end is to affinely shift it down according to the endpoint value in the original random walk.

I don't know of a way to do this in TikZ except to generate the random numbers once to compute what the (random) endpoint would be, and then use this to plot with the affine shift a random walk using the same random numbers.

Unfortunately my code (for the random walk bit, taken from here) which I think should do this does not; the random numbers used to compute the endpoint is different from the ones that are used in the plotting, even though I set the random seed to be the same fixed number before each section. Here is a MWE:

\documentclass[10pt]{article}
\usepackage{tikz}

\usetikzlibrary{calc}

\newcommand{\bb}[5]{% points, advance, rand factor, options, seed
\xdef\y{0}
\pgfmathsetseed{#5} 

\foreach \x in {1,...,#1}
{
    \pgfmathparse{\y + rand*#3} % computing next step of random walk
    \xdef\y{\pgfmathresult}
    \node[circle, fill, inner sep=0pt, outer sep=0pt, minimum size=2mm, scale=0.1] at (\x*#2-3, \y) {}; % to see what random walk is being used to compute endpoint
}

\pgfmathsetseed{#5}
\draw[#4] (-3,0)
\foreach \x in {1,...,#1}
{  -- ++(#2,rand*#3)
};
}

\begin{document}

\begin{tikzpicture}
    \bb{700}{0.02}{0.09}{}{1355}
    \draw (-3,-3) rectangle (11,3);
\end{tikzpicture}
\end{document}

I've excluded the code for computing the affine shift after I have the endpoint \y; this is just to show that I'm getting different plots.

Below is the plot I get (the "ghost" one is the random walk the endpoint is being computed for, and the black one is the one being plotted after):

enter image description here

If there is some other simple way of making a random walk with a desired ending point I would be happy to know that too.

2

I would just store the random numbers in a list. (I guess that the affine transformation could be detectable by some algorithm, but I might well be wrong. And my understanding of random seed is that it depends what you're doing between the random seed and the actual computation of the "random" number, but I may be wrong with this as well.)

\documentclass[10pt]{article}
\usepackage{tikz}

\usetikzlibrary{calc}

\newcommand{\bb}[5]{% points, advance, rand factor, options, seed
\xdef\y{0}
\pgfmathsetseed{#5} 
\xdef\lst{}
\foreach \x [count=\n] in {1,...,#1}
{
    \pgfmathparse{\y + rand*#3} % computing next step of random walk
    \xdef\y{\pgfmathresult}
    \xdef\finaly{\pgfmathresult}
    \xdef\finaln{\n}
    \ifnum\n=1\relax
    \xdef\lst{{\x/\y}}
    \else
    \xdef\lst{\lst,{\x/\y}}
    \fi
    \node[circle, fill, inner sep=0pt, outer sep=0pt, minimum size=2mm, scale=0.1] at (\x*#2-3, \y) {}; % to see what random walk is being used to compute endpoint
}

\typeout{\finaly\space\finaln}
\foreach \x/\y [count=\n] in \lst
{
\pgfmathsetmacro{\newy}{\y-(\n/\finaln)*\finaly}
\node[blue,circle, fill, inner sep=0pt, outer sep=0pt, minimum size=2mm,
scale=0.1] at (\x*#2-3, \newy) {};
}
\draw[#4] (-3,0)
\foreach \x/\y [count=\n] in \lst
{
\pgfextra{\pgfmathsetmacro{\newy}{\y-(\n/\finaln)*\finaly}}
  -- (\x*#2-3,\newy)
};
}

\begin{document}

\begin{tikzpicture}
    \bb{700}{0.02}{0.09}{}{1355}
    \draw (-3,-3) rectangle (11,3);
\end{tikzpicture}
\end{document}

enter image description here

The transformed points are blue and connected.

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.