“10 PRINT” in TikZ

The book 10 PRINT CHR$(205.5+RND(1)); : GOTO 10, by Nick Montfort, et. al., uses a one-line Commodore 64 BASIC program “as a gateway into a deeper understanding of how computing works in society and what the writing, reading, and execution of computer code mean” (p. 4). The focus is on the titular program, which the authors call 10 PRINT for short:

10 PRINT CHR$(205.5+RND(1)); : GOTO 10

This program prints an infinite random sequence made up of the box-drawing characters ╱ and ╲ (that’s U+2571 and U+2572, respectively, encoded in the Commodore character set PETSCII as 205 and 206.), in order to make a random maze. This produces an output similar to the following, except that the characters on a Commodore screen are more square:

(The book’s web site gives a better idea how the output on a Commodore would look.)

It’s not hard to reproduce 10 PRINT in Python, albeit using more than one line:

#! /usr/bin/env python3

import random

for j in range(0, 24):
    print("".join([random.choice(["\u2571","\u2572"])
        for k in range(0, 40)]))

(Instead of an infinite string, my program only outputs a 40 × 24 block of characters.) To get the output to look right, you might need to mess with the font settings in your Terminal. I found that that Melno Regular font on my Mac works.

But I really thought it would be more fun to obtain a 10 PRINT-like output using TikZ, as TikZ is capable of generating random numbers! Instead of using the characters ╱ and ╲, I will draw the lines directly:

\documentclass{standalone}

\usepackage{tikz}

\begin{document}

  \begin{tikzpicture}

    % Draw background
    \fill[blue!75!black] (0, 0) -- (0, 24) --
    (40, 24) -- (40, 0) -- cycle;

    % Draw maze
    \foreach \y in {0, ..., 23}{
      \foreach \x in {0, ..., 39}{

        % Randomly choose a = 0 or a = 1
        \pgfmathrandominteger{\a}{0}{1};

        % If a = 0, then will draw SW-NE line
        % If a = 1, then will draw NW-SE line
        \draw[very thick, white] (\x, \y + \a)
          -- (\x + 1, \y + 1 - \a);
      }
    }

  \end{tikzpicture}

\end{document}

I really like the output:

Adventures in TikZ: tkz-graph

The other day, I was writing some lecture notes for my linear algebra class, and wanted to create the following diagram (to illustrate the concept of a Markov chain):

I had a very limited time in which to finish these notes. Fortunately, I found the tkz-graph package, which made this a snap:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{fouriernc}
\usepackage{tkz-graph}

\begin{document}
\begin{tikzpicture}
\SetGraphUnit{5}

\Vertex[x=0, y=10]{0 points};
\Vertex[x=0, y=5]{1 point};
\Vertex[x=0, y=0]{Win};
\Vertex[x=5, y=5]{Lose};

\Edge[style ={->}, label={$1/3$}]({0 points})({1 point});
\Edge[style ={->}, label={$1/3$}]({1 point})({Win});
\Edge[style ={->}, label={$1/6$}]({0 points})({Lose});
\Edge[style ={->}, label={$1/6$}]({1 point})({Lose});

\Loop[style ={->}, label={$1/2$}, labelstyle={fill=white}]({0 points});
\Loop[style ={->}, label={$1/2$}, labelstyle={fill=white}]({1 point});
\Loop[style ={->}, label={$1$}, dir=EA, labelstyle={fill=white}]({Lose});
\Loop[style ={->}, label={$1$}, labelstyle={fill=white}]({Win});

\end{tikzpicture}
\end{document}

You don’t even have to specify the locations of the vertices; you can throw caution to the wind and have LaTeX decide where to place them! (I am a bit too much of a perfectionist for that.)

One slight issue I had was that the documentation for this package (at least on my computer, as retrieved by texdoc) was in French. Fortunately, I seem to have retained enough knowledge since I took the French language exam as a grad student that I could read most of the documentation.