# How to randomly assign background picture in Beamer?

I am reading the presentation here where the background image changes in each slide. I am thinking how you can do it in Beamer. Pseudocode

\documentclass{beamer}
\usetheme{Berkeley}
% TODO random background generation here

\begin{document}
\usepackage{lipsum}
\begin{frame}
\frametitle{Normal}
% lorem
\end{frame}
\end{document}


If there is a test collection of background images in TeXLive, that would be great to use them. Otherwise, the dummy test image is fine.

OS: Debian 9.1 Stretch
TeXLive: 2017

The following uses pgf/tikz to generate a random number out of 1, 2 and 3. In the mwe package (which is included with xcolor) there are the image files example-image-a, example-image-b, and example-image-c, so we choose them with \@alph.

\documentclass{beamer}
\usetheme{Berkeley}

\usepackage{pgf}% or \usepackage{tikz}
\usepackage{lipsum}

\makeatletter
\newcommand*{\randimg}{%
\pgfmathrandom{3}%
\includegraphics[width=\paperwidth,height=\paperheight]%
{example-image-\@alph{\pgfmathresult}}}
\makeatother

\setbeamertemplate{background}{%
\randimg%
}

\begin{document}
\begin{frame}
\frametitle{Normal}
\lipsum
\end{frame}
\begin{frame}
Foo
\end{frame}
\begin{frame}
Bar
\end{frame}
\begin{frame}
Baz
\end{frame}
\end{document}


The following uses a TeX count called \mycount to cycle through the images and doesn't need pgf or tikz anymore (just the definition of \randimg and the initialization of \mycount, the rest remains untouched):

\newcount\mycount
\newcommand*{\randimg}{%

• @LéoLéopoldHertz준영 your question is titled as "How to randomly assign background picture in Beamer?". My answer does exactly this. If you want to cycle through images, you might increment a counter on each frame and revert it to 1 if it is greater than 3. And instead of \@alph{\pgfmathresult} you use \alph{mycounter}. – Skillmon Sep 8 '17 at 18:05