# High entropy alloy in TikZ

I'm trying to reproduce this illustration of a high entropy alloy (HEA) in TikZ:

What I have so far looks quite bland because I can't find a way to randomly assign one out of a list of colors.

\documentclass[tikz]{standalone}

\def\colors{{red,green,blue,yellow}}

\begin{document}
\begin{tikzpicture}[]
\foreach \i in {1,...,12} {
\foreach \j in {1,...,6} {
\foreach \k in {1,...,4} {
\pgfmathparse{rnd}
\definecolor{randColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
}
}
}
\end{tikzpicture}
\end{document}


### Two questions:

• How can I assign one of \colors{{red,green,blue,yellow}} either randomly or seemingly randomly based on e.g. Mod(num, base) to each of the balls? I was unable to get array indexing to work:

\shade[ball color=\colors[Mod(\i+\j+\k, 4)]


and

\pgfmathparse{\i+\j+\k}


both throw errors.

• Can the viewing angle be rotated so as to resemble the target image?

I think the simplest way is to define your colors in a list with \pgfmathdeclarerandomlist and then you can randomly select an item using \pgfmathrandomitem. Since you have a 2D TikZ image, changing the view means to change the position (coordinates) of the balls.

Code:

\documentclass[tikz]{standalone}

\pgfmathdeclarerandomlist{colors}{%
{red}%
{green}%
{blue}%
{yellow}%
}

\begin{document}
\begin{tikzpicture}[]
\foreach \i in {1,...,12} {
\foreach \j in {1,...,6} {
\foreach \k in {1,...,4} {
\pgfmathrandomitem{\randColor}{colors}
}
}
}
\end{tikzpicture}
\end{document}


By the way, the answers to Drawing 3d crystal lattice with molecular layer in tikz show you other possibilities to build such a cube of "balls". These can be also randomly colored. For instance, for the answer of JLDiaz:

\documentclass{standalone}
\usepackage{tikz}

\pgfmathdeclarerandomlist{colors}{%
{red}%
{green}%
{blue}%
{yellow}%
}

\begin{document}

% You can tweak these
%

\def\DrawRow#1#2{
\foreach \x in {0,...,#2}
\pgfmathrandomitem{\randColor}{colors}
\shade[ball color=\randColor] ($(#1) +(\x, 0,0)$) circle(\ballradius);
}
\def\DrawOddPlane#1{
\pgfmathsetmacro{\aux}{#1-1}
\foreach \z in {0,...,#1} {
\DrawRow{0,0,\z}{#1}
\if\z#1\relax\else
\DrawRow{0.5,0,\z+0.5}{\aux}
\fi
}
}
\def\DrawEvenPlane#1{
\pgfmathsetmacro{\aux}{#1-1}
\foreach \z in {0,...,#1} {
\DrawRow{0.5,0,\z}{\aux}
\if\z#1\relax\else
\DrawRow{0,0,\z+0.5}{#1}
\fi
}
}

\begin{tikzpicture}
\foreach \y in {0,...,3} {
\begin{scope}[yshift=\y cm]
\DrawOddPlane{3}
\end{scope}
\if\y3\relax\else
\begin{scope}[yshift=\y cm + 0.5cm]
\DrawEvenPlane{3}
\end{scope}
\fi
}
\pgfmathsetmacro{\cubex}{1}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{1}
\draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw (3,3,3) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\end{document}


• Cool! I'd never come across \pgfmathdeclarerandomlist and \ pgfmathrandomitem. Just what I needed. Aug 28 '20 at 13:13

With the great help of @Ñako, here's the final result.

% Cartoon of the AlCoCrFeNi high entropy alloy (HEA) with body-centered cubic (BCC) lattice.

\documentclass[tikz]{standalone}

\pgfmathdeclarerandomlist{colors}{{red!80}{teal}{blue!80}{orange}{blue!20}}

\begin{document}
\begin{tikzpicture}
\foreach \i in {1,...,12} {
\foreach \j in {1,...,4} {
\foreach \k in {1,...,4} {
\pgfmathrandomitem{\randColor}{colors}