tikz add tag
topnush
Consider this code that defines 4 colors and lets me draw circles of these 4 colors easily.


```
\documentclass[xcolor={rgb}]{beamer}

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

\usepackage{circledsteps}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand{\mycircle}[1][1]{%
\Circled[fill color={foo!![#1]},outer color=blue!60]{\phantom{\mbox{o}}}}%
\def\numcolors{4}
\pgfmathparse{1/\numcolors}%
\definecolorseries{foo}{hsb}{step}{red!90!black}{\pgfmathresult,0,0} 
\resetcolorseries[\numcolors]{foo}%
\makeatletter
\newcommand{\UncoverList}[2][]{\c@pgf@counta0\relax
\pgfutil@for\my@item:={#2}\do{%
\advance\c@pgf@counta by1%
\only<\c@pgf@counta->{\my@item}%
}}
\makeatother

\begin{document}
\begin{frame}[t, fragile]{}
\mycircle[1]\mycircle[2]\mycircle[3]\mycircle[4]
\end{frame}
\end{document}
```

I would like to show 4 different colored circles allocated randomly to 8 buckets. So the image should have 8 buckets, some of which will be empty and some will have some circles in them. 

Ideally, each time you run the code you should get a different random allocated (unless you set the seed).


Top Answer
samcarter
To make things a bit easier, I'm drawing the buckets as seen from above :)

```
\documentclass[xcolor={rgb}]{beamer}

\usepackage{tikz}
\def\numcolors{4}
\pgfmathparse{1/\numcolors}%
\definecolorseries{foo}{hsb}{step}{red!90!black}{\pgfmathresult,0,0} 
\resetcolorseries[\numcolors]{foo}%
\pgfmathsetseed{\number\pdfrandomseed}

\begin{document}
\begin{frame}
\begin{tikzpicture}

  % drawing buckets
  \foreach \x in {1,...,8}{
    \draw (0.125*\textwidth*\x,0) circle [radius=0.5];
  }
  
  % distribute 4 circles randomly to the buckets
  \foreach \x in {1,...,4}{
    \pgfmathrandominteger{\bucket}{1}{8}% which bucket
    \pgfmathsetmacro{\xran}{rand}% random offset x
    \pgfmathsetmacro{\yran}{rand}% random offset y
    \draw[fill={foo!![\x]},draw=blue!60] (0.125*\textwidth*\bucket+5*\xran,0+0.2*\yran) circle [radius=0.12]; 
  }
  
\end{tikzpicture}
\end{frame}
\end{document}
```

![Screen Shot 2020-09-22 at 11.30.21.png](/image?hash=5fbc0d0c3cec626cd8cd44f52301c3993bf1b6fdd0e060edee51df400805e49c)

And now with more buckety buckets:

```
\documentclass[xcolor={rgb}]{beamer}

\usepackage{tikz}
\def\numcolors{4}
\pgfmathparse{1/\numcolors}%
\definecolorseries{foo}{hsb}{step}{red!90!black}{\pgfmathresult,0,0} 
\resetcolorseries[\numcolors]{foo}%
\pgfmathsetseed{\number\pdfrandomseed}

\begin{document}
\begin{frame}
\begin{tikzpicture}

  % drawing buckets
  \foreach \x in {1,...,8}{
    \draw (0.125*\textwidth*\x,0.4) ellipse [x radius=0.5, y radius=0.3];
    \draw (0.125*\textwidth*\x,0.4)++(-0.5,0) -- ++(0.15,-0.8) arc [start angle=180, end angle=360, x radius=0.35, y radius=0.2] -- ++(0.15,0.8); 
  }
  
  % distribute 4 circles randomly to the buckets
  \foreach \x in {1,...,4}{
    \pgfmathrandominteger{\bucket}{1}{8}% which bucket
    \pgfmathsetmacro{\xran}{rand}% random offset x
    \pgfmathsetmacro{\yran}{rand}% random offset y
    \draw[fill={foo!![\x]},draw=blue!60] (0.125*\textwidth*\bucket+5*\xran,-0.2+0.2*\yran) circle [radius=0.12]; 
  }
  
\end{tikzpicture}
\pause[10]
\end{frame}
\end{document}
```

![document.gif](/image?hash=e1ad81cf7ea3f646d909208d747bdaffbb641b713256c0741140165f330c480b)
Answer #2
marmot
This is just for fun. It draws the buckets in 3d. This part is reasonable, but the computation of the critical angle is just stolen from the [code for the cone](https://topanswers.xyz/tex?q=1264). Then it places the balls avoiding collisions. This also sort of works. However, there are serious drawbacks. Apart from the bulky code there is no 3d ordering in place. Even worse, if the random numbers do not find a solution that does not intersect with previous balls, an error message will be issued. And of course it does not stack the balls if the lowest layer is full. All other parts are stolen from [samcarter's answer](https://topanswers.xyz/tex?q=1337#a1582).

```
\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\tikzset{pics/bucket back/.style={code={%
  \path[fill=gray] circle[radius=Rsmall];
  \path[smooth,left color=gray!10,right color=gray!60!black] 
   plot[domain=\tdplotmainphi+180-\alphacrit:\tdplotmainphi+\alphacrit-360+s]
    ({Rsmall*cos(\x)},{Rsmall*sin(\x)},0) --
   plot[domain=\tdplotmainphi+\alphacrit-360+s:\tdplotmainphi+180-\alphacrit]
    ({R*cos(\x)},{R*sin(\x)},h) -- cycle;
  \path[smooth,left color=gray,right color=gray!10] 
   plot[domain=\tdplotmainphi+\alphacrit-360+s:\tdplotmainphi+\alphacrit-360]
    ({Rsmall*cos(\x)},{Rsmall*sin(\x)},0) --
   plot[domain=\tdplotmainphi+\alphacrit-360:\tdplotmainphi+\alphacrit-360+s]
    ({R*cos(\x)},{R*sin(\x)},h) -- cycle;
  }},
  pics/bucket fore/.style={code={%
  \path[smooth,left color=gray!10,right color=gray] 
   plot[domain=\tdplotmainphi+180-\alphacrit:\tdplotmainphi+\alphacrit-180+s]
    ({Rsmall*cos(\x)},{Rsmall*sin(\x)},0) --
   plot[domain=\tdplotmainphi+\alphacrit-180+s:\tdplotmainphi+180-\alphacrit]
    ({R*cos(\x)},{R*sin(\x)},h) -- cycle;
  \path[smooth,left color=gray!60!black,right color=gray!10] 
   plot[domain=\tdplotmainphi+\alphacrit-180+s:\tdplotmainphi+\alphacrit]
    ({Rsmall*cos(\x)},{Rsmall*sin(\x)},0) --
   plot[domain=\tdplotmainphi+\alphacrit:\tdplotmainphi+\alphacrit-180+s]
    ({R*cos(\x)},{R*sin(\x)},h) -- cycle;
  }},
  bucket/.cd,place bucket/.code={\foreach \XX in {1,...,50}
  {}}}
\begin{document}
\pgfmathsetseed{\number\pdfrandomseed}%
\foreach \ZZZ in {1,...,12}
{\begin{tikzpicture}[declare function={r=0.1;% radius of ball
	R=0.4;% upper radius of bucket
	Rsmall=0.35;% lower radius of 
	h=0.9;% height
	N=8;% number of buckets
	Nc=4;% number of colors/balls
	d=1;% distance between the buckets
	H=R*h/(R-Rsmall);% height of virtual cone
	s=120;% shading
	}]
 \tdplotsetmaincoords{110}{00}
 \begin{scope}[tdplot_main_coords,transform shape]
  \pgfmathtruncatemacro{\Xmax}{N}
  \pgfmathsetmacro{\alphacrit}{90+acos(R*cos(\tdplotmaintheta)/H)}%
  \path foreach \X in {1,...,\Xmax} {(\X*d,0,0) pic{bucket back}};
  \pgfmathtruncatemacro{\numcolors}{Nc}
  \pgfmathparse{1/\numcolors}%
  \definecolorseries{foo}{hsb}{step}{red!90!black}{\pgfmathresult,0,0} 
  \resetcolorseries[\numcolors]{foo}%
  \foreach \Y in {1,...,\numcolors}
  {\pgfmathrandominteger{\ibucket}{1}{\Xmax}% 
  \foreach \Z in {1,...,5000} % maximum 5000 tries
  {\pgfmathsetmacro{\xrnd}{rnd*(Rsmall-r)}
   \pgfmathsetmacro{\yrnd}{rnd*(Rsmall-r)}
   \pgfmathtruncatemacro{\itest}{(sqrt(\xrnd*\xrnd+\yrnd*\yrnd)>Rsmall-r)}
   \edef\jtest{1}
   \ifnum\itest=0
    \edef\jtest{0}
    \ifnum\Y>1
	 \edef\irun{0}
	 \loop
	  \pgfmathtruncatemacro{\jtest}{\jtest+(\ibucket=={\LstPrevPos}[\irun][0])&&%
	  	(sqrt(pow({\LstPrevPos}[\irun][1]-\xrnd,2)+%
			pow({\LstPrevPos}[\irun][2]-\yrnd,2))<2*r)}
	  \edef\irun{\the\numexpr\irun+1}
	  \ifnum\irun<\numexpr\Y-1
	 \repeat
    \fi
   \fi
   \ifnum\numexpr\itest+\jtest=0\relax
    \ifnum\Y=1
     \xdef\LstPrevPos{{\ibucket,\xrnd,\yrnd}}
	\else
	 \xdef\LstPrevPos{\LstPrevPos,{\ibucket,\xrnd,\yrnd}}
	\fi 
	\path (\ibucket*d+\xrnd,\yrnd,r) coordinate (b-\Y);
	\shade[tdplot_screen_coords,ball color={foo!![\Y]}] (b-\Y) circle[radius=r];
    \breakforeach
   \fi
   }
  }
 \path[opacity=0.5] foreach \X in {1,...,\Xmax} {(\X*d,0,0) pic{bucket fore}}; 
 \end{scope}  
 \end{tikzpicture}}
\end{document}
```
![ani.gif](/image?hash=66ef25580d2fbc3057f9ccffac5dbd98b9014d117342328bd83f39fa5b52e14c)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.