4

I am working on plotting a random vector (X, Y) in the plane where X and Y are defined as a roll of the die and X is die1 plus die2 and Y is die1 minus die2.

I am having trouble setting up a foreach loop to handle this task. What I am thinking of (which wont work but maybe with a modification) is

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \foreach[count = \x from 1] \y in {1, 2, ..., 6}{
    \fill[red] (\x + \y, \x - \y) circle[radius = 0.02cm];
  }
\end{tikzpicture}
\end{document}

The reason I am naming the point by (\x + \y, \x - \y) is because I am not sure how to construct this aspect so that it runs through all the options.

5

Maybe...

\documentclass[tikz,border=5]{standalone}
\usepackage{epsdice}
\begin{document}
\tikz[x=.75cm,y=.75cm]
  \foreach \x in {1,...,6}
    \foreach \y in {1,...,6}
      \fill (\x+\y, \x-\y) circle[radius = 0.02cm]
        node [above] {\epsdice\x\ \epsdice\y};
\end{document}

enter image description here

6

I am not entirely sure that I understand what you are trying to do. For example, are there meant to be 6 (random) dice rolls, two rolls or 6 pairs of two dice rolls? As \x is looping over 1,...,6 I think that there might be 6 dice rolls but perhaps this is meant to indicate that standard dice have the numbers from 1 and 6 on the, or not...:)

My first impression was that you wanted something like this (a plot of 6 random dice throws):

enter image description here

Here the dots have been placed randomly using \pgfmathrandominteger using the code:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thick] (0,-0.5)--(0,6.5);
  \draw[thick] (-0.5,0)--(6.5,0);
  \foreach \x in {1, 2, ..., 6}{
    \pgfmathrandominteger{\y}{1}{6}% random number between 1 and 6
    \fill[red] (\x,\y) circle[radius = 2mm];
    \draw[thin](\x,0.2)--(\x,-0.2)node[below]{$\x$};
    \draw[thin](0.2,\x)--(-0.2,\x)node[left]{$\x$};
  }
\end{tikzpicture}
\end{document}

You can explicitly set the seed used by the random generator using \pgfmathsetseed -- the default seed is \time×\year.

I put axes in to make the distribution of points clearer. I also changed your dot size as 0.02cm was way too small.

However, rereading your question you seem to want the coordinates of the plotted points to be the sum and difference of two dice throws, so perhaps you are really looking for this sort plot:

enter image description here

which was created using:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thick] (0,-5.5)--(0,5.5);
  \draw[thick] (-0.5,0)--(12.5,0);
  \foreach \i in {1, 2, ..., 6}{
    \pgfmathrandominteger{\x}{1}{6}
    \pgfmathrandominteger{\y}{1}{6}
    \fill[red] (\x + \y, \x - \y) circle[radius = 2mm];
  }
  \foreach \i in {1,2,...,5} {
    \draw[thin](0.2,\i)--(-0.2,\i)node[left]{$\i$};
    \draw[thin](0.2,-\i)--(-0.2,-\i)node[left]{$-\i$};
  }
  \foreach \i in {1,2,...,12} {
    \draw[thin](\i,0.2)--(\i,-0.2)node[below]{$\i$};
  }
\end{tikzpicture}
\end{document}

If you do this with a large number of points you will of course just fill out all of the points in the integer lattice enclosed by the lines (2,0)--(7,5)--(12,0)--(7,-5)--(2,0).

  • I think @dustin wants two nested foreach loops. – percusse Oct 10 '14 at 6:20

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.