\rand inside \forloop creates identical values

The problem is that you're doing that inside a table cell and TeX doesn't like it very much. It's better to build up the token list before doing the table:

\documentclass{article}
\usepackage{lcg,forloop}
\newtoks\dierckxtoks
\newcounter{row_number}\newcounter{col_number}

\begin{document}

\chgrand[first=0, last=4, counter=kids]
\dierckxtoks={}

\forloop{row_number}{1}{\value{row_number} < 6}{% 
  \forloop{col_number}{1}{\value{col_number} < 5}{%  
     \rand
     \edef\x{\the\dierckxtoks\arabic{kids} &}
     \dierckxtoks\expandafter{\x}%
  }%  
  \rand
  \edef\x{\the\dierckxtoks\arabic{kids} \noexpand\\}
  \dierckxtoks\expandafter{\x}%
}  

\begin{tabular}{rrrrr}
\the\dierckxtoks
\end{tabular}

\end{document}

enter image description here

On the other hand, as shown in my first comment, also 444444444 can be a sequence of random numbers. :-)


The mandatory expl3 solution.

\documentclass{article}
\usepackage{lcg}
\newcounter{randnumb}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\randomtabular}{ O{randnumb} m m m m }
% #1 = counter name (optional default randnumb)
% #2 = lowest value
% #3 = highest value
% #4 = rows
% #5 = columns
 {
  \chgrand[first=#2, last=#3, counter=#1]
  \dierckx_random_tabular:nnn {#1}{#4}{#5}
 }
\tl_new:N \l_dierckx_tabular_tl
\cs_new_protected:Npn \dierckx_random_tabular:nnn #1 #2 #3
 {
  \tl_clear:N \l_dierckx_tabular_tl
  \prg_replicate:nn { #2 }
   {
    \prg_replicate:nn { #3 - 1 }
     {
      \rand
      \tl_put_right:Nx \l_dierckx_tabular_tl { \arabic{#1} & }
     }
    \rand
    \tl_put_right:Nx \l_dierckx_tabular_tl { \arabic{#1} }
    \tl_put_right:Nn \l_dierckx_tabular_tl { \\ }
   }
  \begin{tabular}{*{#3}{r}}
  \l_dierckx_tabular_tl
  \end{tabular}
 }
\ExplSyntaxOff

\begin{document}

\randomtabular{0}{9}{6}{4}

\randomtabular[kids]{0}{4}{5}{5}

\end{document}

enter image description here


A different solution using the random number facility of pgf:

\documentclass{article}
\usepackage{pgf}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\randomtabular}{ m m m m }
% #1 = lowest value
% #2 = highest value
% #3 = rows
% #4 = columns
 {
  \dierckx_random_tabular:nnnn {#1}{#2}{#3}{#4}
 }
\tl_new:N \l__dierckx_tabular_tl
\int_new:N \l__dierckx_random_number_int
\cs_new_protected:Npn \dierckx_random_tabular:nnnn #1 #2 #3 #4
 {
  \tl_clear:N \l_dierckx_tabular_tl
  \prg_replicate:nn { #3 }
   {
    \prg_replicate:nn { #4 - 1 }
     {
      \dierckx_get_rand:nn { #1 } { #2 }
      \tl_put_right:Nx \l_dierckx_tabular_tl { \int_to_arabic:n { \l__dierckx_random_number_int } & }
     }
    \dierckx_get_rand:nn { #1 } { #2 }
    \tl_put_right:Nx \l_dierckx_tabular_tl { \int_to_arabic:n { \l__dierckx_random_number_int } }
    \tl_put_right:Nn \l_dierckx_tabular_tl { \\ }
   }
  \begin{tabular}{*{#3}{r}}
  \l_dierckx_tabular_tl
  \end{tabular}
 }
\cs_new_protected:Npn \dierckx_get_rand:nn #1 #2
 {
  \pgfmathrandominteger{ \l__dierckx_random_number_int } { #1 } { #2 }
 }
\ExplSyntaxOff

\begin{document}

\randomtabular{0}{9}{6}{4}

\bigskip

\randomtabular{0}{4}{5}{5}

\end{document}

There is no optional argument any more, but it doesn't seem to be really necessary.


A variant of the first expl3 solution that pads the number with zeros to have the same length as the highest possible chosen number.

\documentclass{article}
\usepackage{lcg}
\newcounter{randnumb}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\randomtabular}{ O{randnumb} m m m m }
% #1 = counter name (optional default randnumb)
% #2 = lowest value
% #3 = highest value
% #4 = rows
% #5 = columns
 {
  \int_set:Nn \l_dierckx_padto_int { \tl_count:n { #3 } }
  \chgrand[first=#2, last=#3, counter=#1]
  \dierckx_random_tabular:nnn {#1}{#4}{#5}
 }
\tl_new:N \l_dierckx_tabular_tl
\tl_new:N \l_dierckx_temp_tl
\int_new:N \l_dierckx_padto_int

\cs_new_protected:Npn \__dierckx_padnumber:nn #1 #2
 {
  \tl_set:Nx \l_dierckx_temp_tl { \arabic{#1} }
  \tl_set:Nx \l_dierckx_temp_tl
   {
    \prg_replicate:nn { \l_dierckx_padto_int - \tl_count:N \l_dierckx_temp_tl } { 0 }
    \l_dierckx_temp_tl
   }
  \tl_put_right:Nx \l_dierckx_tabular_tl { \l_dierckx_temp_tl #2 }
 }

\cs_new_protected:Npn \dierckx_random_tabular:nnn #1 #2 #3
 {
  \tl_clear:N \l_dierckx_tabular_tl
  \prg_replicate:nn { #2 }
   {
    \prg_replicate:nn { #3 - 1 }
     {
      \rand
      \__dierckx_padnumber:nn { #1 } { & }
     }
    \rand
    \__dierckx_padnumber:nn { #1 } { }
    \tl_put_right:Nn \l_dierckx_tabular_tl { \\ }
   }
  \begin{tabular}{*{#3}{r}}
  \l_dierckx_tabular_tl
  \end{tabular}
 }
\ExplSyntaxOff

\begin{document}

\randomtabular{0}{100000}{6}{4}

\end{document}

enter image description here


I made a quick tikz solution. You may use \pgfmathsetseed{} for repeatable results.

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \foreach \x in {1,...,5}{
        \foreach \y in {1,...,5}{
        \pgfmathrandominteger{\a}{0}{4}
            \node at (\x/2,\y/2){\a};
        }
    }
\end{tikzpicture}


\end{document}

result:

screenshot