为什么我需要等待大约 30 秒或更长时间才能在此代码中生成一组新问题?

为什么我需要等待大约 30 秒或更长时间才能在此代码中生成一组新问题?

这是该问题的后续问题:
从题库生成考试吗?

在@Werner的以下代码中,我很好奇为什么我需要等待大约 30 秒或更长时间才能生成一组新问题?这几秒内发生了什么?为什么LaTeX需要这个时间?

\documentclass{article}
\usepackage{multicol}% Just for this example
\usepackage{filecontents}
\begin{filecontents*}{bankA.tex}
\begin{questionblock}
Question 1
\end{questionblock}
\begin{questionblock}
Question 2
\end{questionblock}
\begin{questionblock}
Question 3
\end{questionblock}
\begin{questionblock}
Question 4
\end{questionblock}
\begin{questionblock}
Question 5
\end{questionblock}
\begin{questionblock}
Question 6
\end{questionblock}
\begin{questionblock}
Question 7
\end{questionblock}
\begin{questionblock}
Question 8
\end{questionblock}
\begin{questionblock}
Question 9
\end{questionblock}
\begin{questionblock}
Question 10
\end{questionblock}
\end{filecontents*}

\usepackage{catchfile,environ,tikz}

\makeatletter% Taken from https://tex.stackexchange.com/q/109619/5764
\def\declarenumlist#1#2#3{%
  \expandafter\edef\csname pgfmath@randomlist@#1\endcsname{#3}%
  \count@\@ne
  \loop
    \expandafter\edef
    \csname pgfmath@randomlist@#1@\the\count@\endcsname
      {\the\count@}
    \ifnum\count@<#3\relax
    \advance\count@\@ne
  \repeat}
\def\prunelist#1{%
  \expandafter\xdef\csname pgfmath@randomlist@#1\endcsname
          {\the\numexpr\csname pgfmath@randomlist@#1\endcsname-1\relax}
  \count@\pgfmath@randomtemp 
  \loop
    \expandafter\global\expandafter\let
    \csname pgfmath@randomlist@#1@\the\count@\expandafter\endcsname
    \csname pgfmath@randomlist@#1@\the\numexpr\count@+1\relax\endcsname
    \ifnum\count@<\csname pgfmath@randomlist@#1\endcsname\relax
      \advance\count@\@ne
  \repeat}
\makeatother

% Define how each questionblock should be handled
\newcounter{questionblock}
\newcounter{totalquestions}
\NewEnviron{questionblock}{}%

\newcommand{\randomquestionsfrombank}[2]{%
  \CatchFileDef{\bank}{#1}{}% Read the entire bank of questions into \bank
  \setcounter{totalquestions}{0}% Reset total questions counters  ***
  \RenewEnviron{questionblock}{\stepcounter{totalquestions}}% Count every question  ***
  \bank% Process file  ***
  \declarenumlist{uniquequestionlist}{1}{\thetotalquestions}% list from 1 to totalquestions inclusive.
  \setcounter{totalquestions}{#2}% Start the count-down
  \RenewEnviron{questionblock}{%
    \stepcounter{questionblock}% Next question
    \ifnum\value{questionblock}=\randomquestion 
      \par% Start new paragraph
      \BODY% Print question
    \fi
  }%
  \foreach \uNiQueQ in {1,...,#2} {% Extract #2 random questions
    \setcounter{questionblock}{0}% Start fresh with question block counter
    \pgfmathrandomitem\randomquestion{uniquequestionlist}% Grab random question from list
    \xdef\randomquestion{\randomquestion}% Make random question available globally
    \prunelist{uniquequestionlist}% Remove picked item from list
    \bank% Process file
  }}

\begin{document}

\begin{multicols}{3}
  \foreach \x in {1,...,6} {
    \bigskip
    \randomquestionsfrombank{bankA.tex}{6}
  }
\end{multicols}

\end{document}

顺便说一句,我自己的文件是波斯语的,xelatex如果它有帮助的话我会使用它。

答案1

每个伪随机数均使用前列腺素F使用由 生成的种子\pgfmathsetseedPGF 手册提到了有关此宏的以下内容:

\pgfmathsetseed{<integer>}

明确设置伪随机数生成器的种子。默认情况下,它设置为\timex的值\year

嗯,很明显,改变伪随机数的唯一影响来自于\time,因为它更有可能在之前发生变化\year;)

那么,什么是\time?根据TeX 按主题分类(部分33.5 时间,第 263 页):

TeX 有四个参数,\year\month\day\time,它们指示当前作业的开始时间。此后,参数不会更新。用户可以随时更改它们。

所有四个参数都是整数;\time参数给出当前作业自午夜开始以来的分钟数。

请注意,\time仅表示分钟(自午夜以来)。因此,它只会每分钟更改一次(属于“每 30 秒或更长时间”;您可能只是在一分钟左右注意到了这种行为)。

要查看其\time样子,请将其添加\showthe\time到您的代码中并检查.log

相关内容