# Generating random conversions

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}

• You have umpteen end-of-lines that generate spaces in the output in the definition of \randGrooth; just three in the definition of \randFloat. Oct 27 '13 at 11:01
• I'm afraid that also lcg adds several spurious spaces. Oct 27 '13 at 11:32

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}{%
\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} I would use \ifcase instead:

\documentclass{article}
\usepackage[first=1,last=7,counter=grooth]{lcg}

\newcommand\randFloat{{%
\reinitrand[first=0, last=100, counter=var]
\rand\arabic{var}.\rand\arabic{var}}}

\rand\gdef\OldRand{\thegrooth}

\newcommand\randGrooth{%
\rand\ifnum\thegrooth=\OldRand \rand \fi
\ifcase\thegrooth mm  % 0
\or mm              % 1
\or cm              % 2
\or dm              % 3
\or m               % 4
\or dam             % 5
\or hm              % 6
\or km              % 7
\fi
\gdef\OldRand{\thegrooth}%
}

\begin{document}

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

\bigskip
\begin{tabular}{ll}
\randFloat\,\randGrooth =& \hspace*{10 ex} \randGrooth\\
\randFloat\,\randGrooth =& \hspace*{10 ex} \randGrooth
\end{tabular}

\bigskip
\begin{tabular}{ll}
\randFloat\,\randGrooth =& \hspace*{10 ex} \randGrooth\\
\randFloat\,\randGrooth =& \hspace*{10 ex} \randGrooth
\end{tabular}
\end{document} • Dear Herbert, I see that your solution works but can you give some more info on the lines '\rand\gdef\OldRand{\thegrooth}' and '\rand\ifnum\thegrooth=\OldRand \rand \fi'? I don't understand how they work. Oct 28 '13 at 6:38
• I do not allow the same dimension for one line .If it is the same, then I run \rand again.If this is no problem for you, you can delete the \fnum ... part
– user2478
Oct 28 '13 at 6:41