\for 循环中的随机数

\for 循环中的随机数

我正在慢慢地理解为年轻学生创建随机算术问题的代码。

此时,我正在尝试创建十个随机加法问题。

为什么编译不通过?错误消息说未定义\TenRandomQuestions,但我认为我已经定义了它...

\documentclass{article}

\usepackage{pgf}

\pgfmathsetseed{\number\pdfrandomseed}

\newcommand*\initVariables
{
 \pgfmathsetmacro{\A}{random(0,10)}
 \pgfmathsetmacro{\B}{random(0,10)}
}

\newcommand*\OneEquation % This macro compiles.
{
\initVariables
{\newcommand{\answer}{\pgfmathprint{int(\A+\B)}}
$\A+\B=\answer$}
}

\newcommand{\TenRandomQuestions} % This macro does NOT compile. The error message says that this command is undefined.
{%
\foreach \x in {0,1,2,...,10}%
 {%
 \OneEquation \\   % How do I incorporate \x into this? Or do I not need to?
 }%
}%


\begin{document}

\TenRandomQuestions

\end{document}

答案1

\documentclass{article}

\usepackage{pgffor}

\pgfmathsetseed{\number\pdfrandomseed}

\newcommand*\initVariables
{
 \pgfmathsetmacro{\A}{random(0,10)}
 \pgfmathsetmacro{\B}{random(0,10)}
}

\newcommand*\OneEquation % This macro compiles.
{
\initVariables
{\newcommand{\answer}{\pgfmathprint{int(\A+\B)}}
$\A+\B=\answer$}
}

\newcommand{\TenRandomQuestions} % This macro does NOT compile. The error message says that this command is undefined.
{%
    \foreach \x in {0,1,2,...,10}%
    {%
        \OneEquation \\   % How do I incorporate \x into this? Or do I not need to?
    }%
}%


\begin{document}

    \TenRandomQuestions

\end{document}

相关内容