生成随机转换

生成随机转换

我正在开发一个系统,用于随机生成一些单位转换练习。学生会得到如下练习

27.89 毫米 = ...... 嗯

我使用随机值生成器和ifthen包实现了这一点。但是这个包的间距出了问题。

这是我使用的代码,其中包括一个表示应有状态的表和一个随机生成的表:

\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}

答案1

此解决方案使用pdftex原语,因此需要pdflatex运行。单元使用siunitx包进行排版,通过 将它们组织成数组\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}

在此处输入图片描述

答案2

我会改用\ifcase

\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}

在此处输入图片描述

相关内容