# How to create the random numbers with PSTricks?

The TikZ code of Schulte Table (Tran Le Nam's author):

\documentclass[12pt,tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\usepackage{xstring}
\begin{document}
\large
\begin{tikzpicture}
\def\n{5}
\def\mylist{}
\pgfmathsetmacro{\numbermax}{\n*\n}
\foreach \x in {1,...,\numbermax}{
\xdef\mylist{\mylist\noexpand{\x\noexpand}}
}
\begin{scope}
\pgfmathsetmacro{\nt}{\n-1}
\foreach \i in {0,...,\nt}
{
\foreach \j in {0,...,\nt}
{ \pgfmathdeclarerandomlist{mynum}{\mylist}
\pgfmathrandomitem{\mynum}{mynum}
\StrSubstitute{\mylist}{{\mynum}}{}[\sublist]
\global\let\mylist\sublist
\pgfmathparse{0.9*rnd+0.3} \pgfmathsetmacro{\maum}{\pgfmathresult}
\pgfmathparse{0.8*rnd} \pgfmathsetmacro{\mauh}{\pgfmathresult}
\definecolor{mau}{rgb}{\maum,\mauh,0.1}
\node[color=mau] at ($(\i+0.5, \j+0.5 )$) {\mynum};
}
}
\draw (0,0) grid (\n,\n);
\end{scope}
\end{tikzpicture}
\end{document}


Truly, until now, I don't know about "the random syntax" in any PSTricks documentations. Can I find "the random syntax" in where?

Update:

\documentclass[12pt,pstricks,border=5pt]{standalone}
\usepackage{pst-math}
\usepackage{multido}
\begin{document}
\newcounter{RandNo}
\setcounter{RandNo}{1}
\def\n{7} \def\m{10}
\def\N{\the\numexpr\m*\n}
\defineRandIntervall(1,\N){\N}
\makeRandomNumberList
%\resetcolorseries[\N]{test}

\begin{pspicture}(\m,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\multido{\rRow=0.5+1.0}{\n}{\multido{\rCol=0.5+1.0}{\m}{%
\rput(\rCol,\rRow){\getNumberFromList{\theRandNo}}%
\stepcounter{RandNo}}}
\end{pspicture}
\end{document}


It produces figure normally:

BUT

\documentclass[12pt,pstricks,border=5pt]{standalone}
\usepackage{pst-math}
\usepackage{multido}
\definecolorseries{test}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
\begin{document}
\newcounter{RandNo}
\setcounter{RandNo}{1}
\def\n{7} \def\m{10}
\def\N{\the\numexpr\m*\n}
\defineRandIntervall(1,\N){\N}
\makeRandomNumberList
\resetcolorseries[\N]{test}

\begin{pspicture}(\m,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\multido{\rRow=0.5+1.0}{\n}{\multido{\rCol=0.5+1.0}{\m}{%
\rput(\rCol,\rRow){\textcolor{test!!+}{\getNumberFromList{\theRandNo}}%
\stepcounter{RandNo}}}
\end{pspicture}
\end{document}


Compiler gets no result .... :((

Does not work with xelatex (it doesn't know the random function)

\documentclass[12pt]{article}
\usepackage{pstricks,pst-calculate,multido}
\usepackage{auto-pst-pdf}
\definecolorseries{test}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
\begin{document}

\def\n{5}
\begin{pspicture}(\n,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\resetcolorseries[25]{test}
\multido{\rRow=0.5+1.0}{\n}{%
\multido{\rCol=0.5+1.0}{\n}{%
\rput(\rCol,\rRow){\textcolor{test!!+}{\pscalculate{randint(1,25)}}}
}}
\end{pspicture}

\end{document}


And a solution with single random numbers. The random stuff is part of an up-to-date pst-math:

\documentclass[12pt]{article}
\usepackage{pstricks,pst-math}
\usepackage{multido}
\usepackage{auto-pst-pdf}
\definecolorseries{test}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
\begin{document}

\newcounter{RandNo} \setcounter{RandNo}{1}
\def\n{5} \def\N{\the\numexpr\n*\n}
\defineRandIntervall(1,\N){\N}
\makeRandomNumberList
\resetcolorseries[25]{test}

\begin{pspicture}(\n,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\multido{\rRow=0.5+1.0}{\n}{\multido{\rCol=0.5+1.0}{\n}{%
\rput(\rCol,\rRow){\textcolor{test!!+}{\getNumberFromList{\theRandNo}}}%
\stepcounter{RandNo}}}
\end{pspicture}

\end{document}


for big squares it makes more sense to use luatex for doing the calculation. Run the following example with lualatex --shell-escape <file> It needs the package luarandom, available in the next few days for TeXLive or MikTeX. Or here: http://comedy.dante.de/~herbert/TeXnik/tex/lualatex/luarandom/

\documentclass[12pt]{article}
\usepackage{xcolor,pstricks}
\usepackage{multido}
\usepackage{pst-math}
\usepackage{auto-pst-pdf-lua}
\ifpdf\else\usepackage[T1]{fontenc}\fi
\begin{document}

\newcounter{RandNo} \setcounter{RandNo}{1}
\def\n{10} \def\N{\the\numexpr\n*\n}
\makeRandomNumberList{1}{\N}{\N}

\begin{pspicture}(\n,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\multido{\rRow=0.5+1.0}{\n}{\multido{\rCol=0.5+1.0}{\n}{%
\edef\RNumber{\getNumberFromList{\theRandNo}}%
\rput(\rCol,\rRow){\textcolor{randomhsb}{\RNumber}%
\stepcounter{RandNo}}}}
\end{pspicture}

\end{document}


• What is the syntax random in your answer? – user173875 Dec 12 '18 at 11:33
• \pscalculate{randint(1,25)} get an integer random between 1 and 25 – user2478 Dec 12 '18 at 11:36
• If you add these syntax into pst-math package, you also add its guide. Extension for n>5 are very exciting. :-)) – user173875 Dec 13 '18 at 8:39
• Compiler gets no result .... :(( is a totally useless statement! You should provide an error message or a wrong output! Control your parantheses, which can easily be done in an editor where corresponding {...} are highlited. – user2478 Dec 16 '18 at 8:18
• – user2478 Dec 16 '18 at 12:21

A pure PSTricks solution is also possible but we should not reinvent the wheel.

\documentclass[12pt,pstricks,border=1pt]{standalone}
\usepackage{tikz,xstring}
\usetikzlibrary{calc}
\def\n{5}\def\mylist{}

\pgfmathsetmacro{\numbermax}{\n*\n}
\foreach \x in {1,...,\numbermax}{
\xdef\mylist{\mylist\noexpand{\x\noexpand}}
}
\pgfmathsetmacro{\nt}{\n-1}
\newpsstyle{gridstyle}{griddots=0,subgriddiv=1}
\begin{document}
\large
\begin{pspicture}[showgrid](\n,\n)
\foreach \i in {0,...,\nt}
{
\foreach \j in {0,...,\nt}
{   \pgfmathdeclarerandomlist{mynum}{\mylist}
\pgfmathrandomitem{\mynum}{mynum}
\StrSubstitute{\mylist}{{\mynum}}{}[\sublist]
\global\let\mylist\sublist
\pgfmathparse{0.9*rnd+0.3} \pgfmathsetmacro{\maum}{\pgfmathresult}
\pgfmathparse{0.8*rnd} \pgfmathsetmacro{\mauh}{\pgfmathresult}
\definecolor{mau}{rgb}{\maum,\mauh,0.1}
}
}
\end{pspicture}
\end{document}


Rand produces a floating point random number between 0 and 1 inclusive and rand produces a "huge" integer.
\documentclass[pstricks,border=12pt,12pt]{standalone}