1

I would like to have 5 rows of randomly colored circles. Here is my MWE:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\setbeamersize{text margin left=10mm,text margin right=5mm} 
\setbeamertemplate{frametitle}[default][center]
\usepackage{tikz}

\newcommand{\tikzcircle}[2][red,fill=red]{\tikz[baseline=-0.5ex]\draw[#1,radius=#2] (0,0) circle ;}%


\begin{document}
\begin{frame}{The counting problem}
\foreach \x in {1,2,...,100}  {\tikzcircle{2pt}}
\end{frame}

\end{document}

I am not sure how to pick the colors at random or how to get the circles to wrap so they fill the space the text would normally fill.

If possible, it would be great if I could pick colors randomly with replacement from a set whose size I can specify. E.g pick the colors randomly with replacement from a set of 10 colors.

2 Answers 2

3

You can start with something like this. Code for \randomcolor was taken from Generate random color in way that works with both pdflatex and lualatex

\documentclass{beamer}

\beamertemplatenavigationsymbolsempty
\setbeamersize{text margin left=10mm,text margin right=5mm} 
\setbeamertemplate{frametitle}[default][center]
\usepackage{tikz}

\newcommand{\randomcolor}{%
    \pgfmathsetmacro{\R}{random(0,10000)/10000}%
    \pgfmathsetmacro{\G}{random(0,10000)/10000}%
    \pgfmathsetmacro{\B}{random(0,10000)/10000}%
    \definecolor{randomcolor}{rgb}{\R,\G,\B}%
}

\newcommand{\tikzcircle}[1]{\randomcolor\tikz[baseline=-0.5ex]{\fill[randomcolor,radius=#1]circle;}}%


\pgfmathsetseed{\number\pdfrandomseed} % seed for random generator


\begin{document}

\begin{frame}{The counting problem}

\foreach \j in {1,2,...,5} {\foreach \i in {1,2,...,10} {\tikzcircle{.05\textwidth}}\\}

\end{frame}
\end{document}

enter image description here

Update: limited colors

Following code shows an alternative to select colors from a limited set. The set is defined into a TikZ array and the color is selected with a random index.

\documentclass{beamer}

\beamertemplatenavigationsymbolsempty
\setbeamersize{text margin left=10mm,text margin right=5mm} 
\setbeamertemplate{frametitle}[default][center]
\usepackage{tikz}

\def\mycolors{{"red","green","blue","brown","pink","violet","orange","cyan","yellow"}}
    
\newcommand{\tikzcircle}[1]{%
    \pgfmathsetmacro{\mycolorindex}{random(0,8)}%
    \pgfmathsetmacro{\mycolor}{array(\mycolors,\mycolorindex)}%
    \tikz[baseline=-0.5ex]{\fill[\mycolor,radius=#1]circle;}}%

\pgfmathsetseed{\number\pdfrandomseed} % seed for random generator

\begin{document}

\begin{frame}{The counting problem}

\foreach \j in {1,2,...,5}{\foreach \i in {1,2,...,10}{\tikzcircle{.05\textwidth}}\\}

\end{frame}
\end{document}

enter image description here

4
  • Can you specify how many different colors there will be? I want to limit it to a small number sometimes so I will get a lot of repeated colors.
    – Simd
    Commented Sep 1, 2020 at 17:38
  • Try `` \newcommand{\randomcolor}{% \pgfmathsetmacro{\R}{random(0,1)/1}% \pgfmathsetmacro{\G}{random(0,1)/1}% \pgfmathsetmacro{\B}{random(0,1)/1}% \definecolor{randomcolor}{rgb}{\R,\G,\B}% }`` Commented Sep 1, 2020 at 22:31
  • @Anush I've updated the code with a new version for limited colors. This example is made with 9 colors, but you can change the number or just select from a shorter list fixing the upper bound at the random selector.
    – Ignasi
    Commented Sep 2, 2020 at 7:46
  • Thank you very much.
    – Simd
    Commented Sep 2, 2020 at 12:57
2

Try to play with the parameters of the random number generator, while keeping the same seed.

For example

\documentclass{beamer}

\beamertemplatenavigationsymbolsempty
\setbeamersize{text margin left=10mm,text margin right=5mm} 
\setbeamertemplate{frametitle}[default][center]
\usepackage{tikz}

\newcommand{\randomcolor}{%
    \pgfmathsetmacro{\R}{random(0,1)/2}%
    \pgfmathsetmacro{\G}{random(0,1)/2}%
    \pgfmathsetmacro{\B}{random(0,1)/1}%
    \definecolor{randomcolor}{rgb}{\R,\G,\B}%
}

\newcommand{\tikzcircle}[1]{\randomcolor\tikz[baseline=-0.5ex]{\fill[randomcolor,radius=#1]circle;}}%


%\pgfmathsetseed{\number\pdfrandomseed} % seed for random generator


\begin{document}

\begin{frame}{The counting problem}

\foreach \j in {1,2,...,5}{\foreach \i in {1,2,...,10}{\noindent\tikzcircle{.05\textwidth}}\\}

\end{frame}
\end{document}
    

will produce 2x2x2 = 8 different colors. (R= 0 or 0.5, etc.)

fewer colors

1
  • Can you explicitly specify the number of colours you want? In this case it is 6 but say I wanted 10 or some other number.
    – Simd
    Commented Sep 2, 2020 at 6:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .