Random integers with differing probabilities
\documentclass{article}
\usepackage{pgffor}
\newcommand{\myrandom}{%
\expandafter\domyrandom\pdfuniformdeviate 10 \domyrandom
}
\def\domyrandom#1\domyrandom{%
\ifcase#1
1\or
2\or
2\or
3\or
3\or
3\or
4\or
4\or
4\or
4\fi
}
\begin{document}
\foreach \x in {1,...,100}{\myrandom\space}
\newcounter{1}\newcounter{2}\newcounter{3}\newcounter{4}
\foreach \x in {1,...,1000}{\stepcounter{\myrandom}}
1: \the\value{1}\par
2: \the\value{2}\par
3: \the\value{3}\par
4: \the\value{4}\par
\end{document}
You could do a variation of my answer to your previous question, and nest three ifthenelse
s.
\documentclass[border=4mm]{article}
\usepackage{pgffor}
\begin{document}
\foreach\x in {1,...,2000} {
\pgfmathsetmacro{\tmp}{rnd}
\pgfmathparse{ifthenelse(\tmp<=0.1,1,ifthenelse(\tmp<=0.3,2,ifthenelse(\tmp<=0.6,3,4)))}\pgfmathresult
}
\end{document}
Borrowing a bit from egreg's answer:
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\newcounter{1}\newcounter{2}\newcounter{3}\newcounter{4}
\foreach\x in {1,...,2000} {
\pgfmathsetmacro{\tmp}{rnd}
\pgfmathparse{ifthenelse(\tmp<=0.1,1,ifthenelse(\tmp<=0.3,2,ifthenelse(\tmp<=0.6,3,4)))}\pgfmathresult
\stepcounter{\pgfmathresult}
}
1: \the\value{1}\par
2: \the\value{2}\par
3: \the\value{3}\par
4: \the\value{4}\par
\end{document}
While I'm sure there's a direct method to generate integers from 1 to 4 so that they have probabilities 0.1, 0.2, 0.3, and 0.4, an indirect or two-step method is more straightforward to set up. First, generate an integer between 1 and 10 randomly. (I.e., each integer has P=0.1
.) Second, check if the integer is less than 2, 4, and 7, respectively, and assign the numbers "1", "2", "3" accordingly -- and associate the number "4" with the "none of the above" category, i.e. if the integer is between 7 and 10.
Here's a LuaLaTeX-based implementation of this idea.
\documentclass{article}
\newcommand\x{%
\directlua{x=math.random(10) % draw an integer between 1 and 10
if x<2 then tex.sprint(1) % true if x==1
elseif x<4 then tex.sprint(2) % true if x==2 or 3
elseif x<7 then tex.sprint(3) % true if x==4, 5, or 6
else tex.sprint(4) % true if x==7, 8, 9, or 10
end}}
\begin{document}
\obeylines % just for this example
\x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x
\x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x
\x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x
\x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x
\end{document}