[Tex/LaTex] Generating random conversions

conditionalsrandom numbers

I am working on a system to randomly generate some exercises on unit conversion.
The students get a exercise like

27.89 mm = …… hm

I achieved this whit the use of a random value generator and the ifthen package. But something is going wrong whit the spacing of this package.

This is the code I use whit one table of how it should be and one table that is randomly generated:

\documentclass{article}

\newcommand{\randFloat}{
    \reinitrand[first=0, last=100, counter=var]
    \rand\arabic{var}.\rand\arabic{var}
    }
\newcommand{\randGrooth}{
    \reinitrand[first=1,last=7,counter = grooth]
    \rand
    \ifthenelse{\arabic{grooth} = 1}{mm}{
        \reinitrand[first=1,last=6,counter = test]
        \rand
        \ifthenelse{\arabic{test} = 1}{cm}{
            \reinitrand[first=1,last=5,counter = test]
            \rand
            \ifthenelse{\arabic{test} = 1}{dm}{
                \reinitrand[first=1,last=4,counter = test]
                \rand
                \ifthenelse{\arabic{test} = 1}{m}{
                    \reinitrand[first=1,last=3,counter = test]
                    \rand
                    \ifthenelse{\arabic{test} = 1}{dam}{
                        \reinitrand[first=1,last=2,counter = test]
                        \rand
                        \ifthenelse{\arabic{test} = 1}{hm}{km}
                    }
                }
            }
        }
    }
}

\usepackage{calc}
\usepackage{ifthen}
\usepackage{lcg}

\begin{document}

\begin{table}
\centering
\begin{tabular}{ll}
44.36 mm =& \hspace*{10 ex} m\\
56.28 dm=& \hspace*{10 ex} km
\end{tabular}
\end{table}

\begin{table}
\centering
\begin{tabular}{ll}
\randFloat \randGrooth =& \hspace*{10 ex} \randGrooth\\
\randFloat \randGrooth =& \hspace*{10 ex} \randGrooth  
\end{tabular}
\end{table}

\end{document}

Best Answer

This solution uses pdftex primitives, so it needs pdflatex to run. The units are typeset with siunitx package, they are organized into an array by means of \csname.

\documentclass{article}
\usepackage{siunitx}
\sisetup{round-mode = places,round-precision = 2}
%
\pdfsetrandomseed12345 %
%
\newcommand{\randFloat}{\pdfuniformdeviate100\expandafter.\pdfuniformdeviate100 }
%
\expandafter\def\csname0\endcsname{\si{\milli\metre}}
\expandafter\def\csname1\endcsname{\si{\centi\metre}}
\expandafter\def\csname2\endcsname{\si{\deci\metre}}
\expandafter\def\csname3\endcsname{\si{\metre}}
\expandafter\def\csname4\endcsname{\si{\deca\metre}}
\expandafter\def\csname5\endcsname{\si{\hecto\metre}}
\expandafter\def\csname6\endcsname{\si{\kilo\metre}}
%
\newcounter{ucount}
\newcommand{\incUcount}{%
\addtocounter{ucount}{\pdfuniformdeviate6 }%
\ifnum\arabic{ucount}>6 \addtocounter{ucount}{-7}\fi %
\expandafter\csname\arabic{ucount}\endcsname}

\newcommand{\randGrooth}{%
\incUcount\stepcounter{ucount} &= \hspace*{6 em}\incUcount\setcounter{ucount}{0}%
}

\begin{document}

\begin{table}
\centering
\begin{tabular}{Sll}
\randFloat & \randGrooth \\ 
\randFloat & \randGrooth \\ 
\randFloat & \randGrooth \\ 
\randFloat & \randGrooth \\ 
\randFloat & \randGrooth \\ 
\randFloat & \randGrooth \\ 
\randFloat & \randGrooth \\ 
\randFloat & \randGrooth \\ 
\randFloat & \randGrooth \\ 
\randFloat & \randGrooth    
\end{tabular}

\end{table}
\end{document}

enter image description here

Related Question