尝试使用 \edef 时出错

尝试使用 \edef 时出错

请跳过此解释,直接看下面的代码。我的问题是尝试\Q@Selection使用 edef。

我有一个宏,它使用 for 循环来分配一些动态宏,我正在尝试弄明白某些事情是如何工作的。目标是获取一堆使用 for 循环生成的宏,然后对它们进行打乱,然后构建一组对它们进行打乱的宏。

我有一个命令\Knuth@Shuffle{Max#}{Name}{Pick#},它生成(pick#)计数器,名为NameI,,NameII...,,Name\Roman{Pick#}每个计数器包含一个随机(非重复)数字1 - Max#

然后,我想通过pick#从预先生成的宏列表中选择宏来分配一组新宏,宏的形式Question@\Roman{(number)}为数字1Max#。但我想随机挑选,所以我使用:Question@\Roman{Name\Roman{(number)}}来拉取随机选择。由于我需要在循环内分配这些值,我需要使用来\edef确保在循环运行时分配值,而不是在循环结束时分配值(实际上给了我很多次相同的宏)

代码:

失败的功能:

\newcommand{\Q@Selection}{%     The Command that Selects and stores Qs
\setcounter{Iteration@Select}{1}
%
\Knuth@Shuffle{\arabic{Problem@MasterCounter}}{Q@Pulled}{\arabic{Real@PullNumber}}%             Start For Loop
\forloop{Iteration@Select}{1}{\arabic{Iteration@Select} < \arabic{Real@PullNumber}}%    Iterate for each desired question
    {%                                  Start Iteration Step
    \expandafter\edef\csname Q@\Roman{Iteration@Select}\endcsname{\csname Question@\Roman{Q@Pulled\Roman{Iteration@Select}}\endcsname}% Select the question that was the "Q@Pull" number from the start. Currently capable of pulling the same question twice.
    }%                                  End Iteration Step
%                                   End for Loop
}

如果您想了解随机化函数的工作原理,请参阅支持代码:

\newcommand{\@genrand}[3] %\@genrand{NAME}{MIN}{MAX} generates a random number before MIN and MAX and stores it in the command \NAME.
{
\expandafter\pgfmathrandominteger\csname #1\endcsname{#2}{#3}
\setcounter{#1}{\csname #1\endcsname}
}

\newcommand{\Knuth@Shuffle}[3]% \Knuth@Shuffle{MAXCOUNT}{NAME}{USEDCOUNT} Generates and permutes a list MAXCOUNT number of counters, each with name NAMEI, NAMEII, NAMEIII, ..., NAME\Roman(MAXCOUNT). Then it stores up to USEDCOUNT of those counters in counters named 
{
%Assign a maximum on how many numbers to pick. Set default to the max list size, and save in counter "RndQuant"
\ifthenelse{\isempty{#3}}%          Check to see if Desired Maximum is given.
    {
    \setcounter{Rnd@Quant}{#1+1}        %If not, use all of them.
    }
    {
    \ifthenelse{#1 < #3}
        {
        \setcounter{Rnd@Quant}{#1+1}        %Only select up to all problems.
        }
        {
        \setcounter{Rnd@Quant}{#3+1}        
        }
    }

\setcounter{Rnd@EndQuant}{#1+1}

%Generate a starting list of numbers 1 to maximum number given.
\forloop{Iteration@1}{1}{\arabic{Iteration@1} < \arabic{Rnd@EndQuant}}
    {
    \@ifundefined{c@#2\Roman{Iteration@1}}  % Check to see if counter exists
        {                                   %If not, make it.
        \newcounter{#2\Roman{Iteration@1}}
        }
        {}                                  %If so, do nothing.
    \setcounter{#2\Roman{Iteration@1}}{\arabic{Iteration@1}}    % Set Counter to next number.
    }

%Permute using Knuth method
\forloop{Iteration@2}{1}{\arabic{Iteration@2} < \arabic{Rnd@Quant}}
    {
    \@genrand{Temp@RandMe}{\arabic{Iteration@2}}{#1}%   Generate a random number from Current iterate number to maximum number.

    \setcounter{Temp@Hold}{\arabic{#2\Roman{Temp@RandMe}}}% Set a temp variable so that we can swap values in position of the iterate and the random number selected. 

    \setcounter{#2\Roman{Temp@RandMe}}{\arabic{#2\Roman{Iteration@2}}}% Set the final counter to the value of the variable in the generated number's counter.
    \setcounter{#2\Roman{Iteration@2}}{\arabic{Temp@Hold}}% Set current counter to the swapped counter.
}
}

作为脚注:我写这篇文章主要是为了学习如何做某些事情,但如果有人能提出更简洁、更友好的建议,我也会非常感激。我喜欢自学,但这并不意味着我会忽视好的建议。

答案1

Knuth 提供的 TeX 引擎确实只有 256 个计数寄存器。
因此,尽可能避免永久保留计数寄存器。

\expandafter\pgfmathrandominteger\csname #1\endcsname{#2}{#3}确实意味着赋值,而\edef仅进行扩展,这意味着 的定义文本中的赋值\edef不会在定义时执行。相反,相应的标记会保留在原处,并成为即将由 定义的宏的定义文本的一部分。这意味着每当 定义的宏被执行/扩展\edef时,这些标记就会被“吐出” 。\edef

如果我理解正确的话,您希望按时间顺序随机调用一定数量的不同宏,这些宏的名称可以从一定范围内的整数中派生出来。任何宏都不应被调用两次。

LaTeX 的一个问题是,通常需要进行多次编译才能使所有内容匹配,因为诸如、、超链接等机制\tableofcontents依赖\label..\ref于上一次 LaTeX 编译期间收集的信息。

如果使用随机数随机创建文本部分,则每个 LaTeX 编译都可能提供具有其他分页符的另一段文本\tableofcontents,而\label..\ref、 、超链接等则依赖于上一个 LaTeX 编译期间收集的信息。
如果您对此不采取任何措施,这可能会导致一个永无止境的故事,因为上一个 LaTeX 编译期间收集的信息永远不会与当前 LaTeX 编译中的页码等相对应。

因此,拥有一种将随机值存储在 .aux 文件中并使其类似于 \label..\ref- 机制的机制似乎是个好主意。

由于答案限制为 30,000 个字符,我决定在其他答案中发布编码示例:

示例 1 可在此处找到:https://tex.stackexchange.com/a/344282/118714
示例 2 可在此处找到:https://tex.stackexchange.com/a/344287/118714

答案2

编码示例 1 回答https://tex.stackexchange.com/a/343436/118714

以下代码能实现您想要的效果吗?

\documentclass{article}
\usepackage{pgf}
%
\makeatletter
%
\newcommand\exchange[2]{#2#1}%
\newcommand\firstoftwo[2]{#1}%
\newcommand\secondoftwo[2]{#2}%
%
\newcommand\Name{}\long\def\Name#1#{\Innername{#1}}%
\newcommand\Innername[2]{%
  \expandafter\exchange\expandafter{\csname#2\endcsname}{#1}%
}%
%
\newcommand\innerdfork{}\def\innerdfork#1d#2#3dd{#2}%
\newcommand*\dfork[1]{\innerdfork#1{\firstoftwo}d{\secondoftwo}dd}%
%
%%......................................................................
%% \RetrieveRandomfromlabel{<name of label>}%
%%                         {<macro>}%
%%                         {<lower bound>}%
%%                         {<upper bound>}%
%%                         {<amount>}%
%%                         {<action in case label is undefined or changed>}%
\newcommand\RetrieveRandomfromlabel[6]{%
  \@ifundefined{@random@#1}{\firstoftwo}{%
    \ifnum\Name
          \expandafter\expandafter
          \expandafter\firstoftwo
          \expandafter\secondoftwo{@random@#1}=#3 %
    \expandafter\firstoftwo\else\expandafter\secondoftwo\fi
    {%
      \ifnum\Name
            \expandafter\expandafter\expandafter\expandafter
            \expandafter\expandafter\expandafter\firstoftwo
            \expandafter\expandafter\expandafter\secondoftwo
            \expandafter\secondoftwo{@random@#1}=#4 %
      \expandafter\firstoftwo\else\expandafter\secondoftwo\fi
      {%
        \ifnum\Name
              \expandafter\expandafter\expandafter\expandafter
              \expandafter\expandafter\expandafter\secondoftwo
              \expandafter\expandafter\expandafter\secondoftwo
              \expandafter\secondoftwo{@random@#1}=#5 %
        \expandafter\secondoftwo\else\expandafter\firstoftwo\fi
      }{\firstoftwo}%
    }%
    {\firstoftwo}%
  }%
  {#6}%
  {%
    \expandafter\expandafter\expandafter\expandafter
    \expandafter\expandafter\expandafter\newcommand
    \expandafter\expandafter\expandafter\expandafter
    \expandafter\expandafter\expandafter#2%
    \expandafter\expandafter\expandafter\expandafter
    \expandafter\expandafter\expandafter{%
    \expandafter\expandafter\expandafter\firstoftwo
    \csname @random@#1\endcsname}%
  }%
}%
\newcommand\newrandomlabel{\@newl@bel{@random}}%
%% \WriteRandomMacrotolabel{<macro>}%
%%                         {<label>}%
%%                         {<lower bound>}%
%%                         {<upper bound>}%
%%                         {<amount>}%
\newcommand\WriteRandomtolabel[5]{%
  \protected@write\@auxout{}{\string\newrandomlabel{#2}{{#1}{{#3}{{#4}{#5}}}}}%
}%
%
\newcommand\createlistofdifferentrandomintegers[5]{%
  \ifnum#2<1 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {%
    \@latex@error{Lower bound is not a positive number}%
                 {Lower bound must a positive number.}%
  }{%
    \ifnum#2>#3 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
    {\@latex@error{Lower bound greater than upper bound}%
                  {Lower bound must not be greater than upper bound.}%
    }{%
      \begingroup
      \@tempcnta=#3\relax
      \advance\@tempcnta by 1%
      \advance\@tempcnta -#2\relax
      \expandafter\endgroup
      \expandafter\ifnum\the\@tempcnta<#1 %
        \expandafter\@firstoftwo
      \else
        \expandafter\@secondoftwo
      \fi
      {\@latex@error{Amount of elements to randomize on is
                     smaller than amount of elements to create}%
                    {Finding more unequal numbers than there are numbers
                     contained in the set from which numbers are to be
                     chosen randomly is not possible. See Dirichlet's
                     drawer principle.}%
      }{%
        \RetrieveRandomfromlabel{#4}{#5}{#2}{#3}{#1}{%
          \expandafter\exchange
          \expandafter{%
          \romannumeral\number\number#1 000d}{%
            \innercreatelistofdifferentrandomintegers{}{#2}{#3}{#5}%
          }%
        }%
        \WriteRandomtolabel{#5}{#4}{#2}{#3}{#1}%
      }%
    }%
  }%
}%
%
\newcommand\innercreatelistofdifferentrandomintegers[5]{%
  \dfork{#5}{\newcommand#4{#1}}{%
    \CreateRandomNumbersUntilNumberIsNotInList{#1}{#2}{#3}{#4}%
  }%
}%
%
\newcommand\mytempa{}%
\newcommand\CreateRandomNumbersUntilNumberIsNotInList[3]{%
  \pgfmathrandominteger\mytempa{#2}{#3}%
  \CheckIfNumMyTempaIsAlreadyInList#1{}{}\relax
  {%
    \CreateRandomNumbersUntilNumberIsNotInList{#1}{#2}{#3}%
  }{%
    \expandafter\innercreatelistofdifferentrandomintegers
    \expandafter{%
    \expandafter{%
    \mytempa}#1}{#2}{#3}%
  }%
}%
\newcommand\CheckIfNumMyTempaIsAlreadyInList{}%
\long\def\CheckIfNumMyTempaIsAlreadyInList#1#2\relax{%
  \ifx\relax#1\relax
    \expandafter\firstoftwo
  \else
    \expandafter\secondoftwo
  \fi
  {\secondoftwo}{%
     \ifnum\mytempa=#1 %
       \expandafter\firstoftwo
     \else
       \expandafter\secondoftwo
     \fi
     {\firstoftwo}%
     {\CheckIfNumMyTempaIsAlreadyInList#2\relax}%
  }%
}%
%
%
\newcommand\DoWithEachElementOfIntegerlist[2]{%
  \expandafter\exchange
  \expandafter{%
  #1}{\InnerDoWithEachElementOfIntegerlist{#2}}\relax
}%
\newcommand\InnerDoWithEachElementOfIntegerlist[2]{%
  \ifx\relax#2%
    \expandafter\firstoftwo
  \else
    \expandafter\secondoftwo
  \fi
  {}%
  {#1{#2}\InnerDoWithEachElementOfIntegerlist{#1}}%
}%
%
%
\newcommand\CallMacroNumber[1]{%
  \Name\string{Macro#1}:~%
  \Name{\@ifundefined{Macro#1}{Undefined}{Macro#1}}%
}%
\makeatother

\begin{document}

%Define twenty macros whose names contain the prefix "Macro" and arabic digits:
\Name\newcommand{Undefined}{!!Undefined!!\\}
\Name\newcommand{Macro1}{Number One\\}
\Name\newcommand{Macro2}{Number Two\\}
\Name\newcommand{Macro3}{Number Three\\}
\Name\newcommand{Macro4}{Number Four\\}
\Name\newcommand{Macro5}{Number Five\\}
\Name\newcommand{Macro6}{Number Six\\}
\Name\newcommand{Macro7}{Number Seven\\}
\Name\newcommand{Macro8}{Number Eight\\}
\Name\newcommand{Macro9}{Number Nine\\}
\Name\newcommand{Macro10}{Number Ten\\}
\Name\newcommand{Macro11}{Number Eleven\\}
\Name\newcommand{Macro12}{Number Twelve\\}
\Name\newcommand{Macro13}{Number Thirteen\\}
\Name\newcommand{Macro14}{Number Fourteen\\}
\Name\newcommand{Macro15}{Number Fifteen\\}
\Name\newcommand{Macro16}{Number Sixteen\\}
\Name\newcommand{Macro17}{Number Seventeen\\}
\Name\newcommand{Macro18}{Number Eighteen\\}
\Name\newcommand{Macro19}{Number Nineteen\\}
\Name\newcommand{Macro20}{Number Twenty\\}


% This creates a list of eleven different random numbers in range of 1..20
% and stores that list in the macro \ListOfRandomNumbers:
% In consecutive LaTeX-runs when aux-file exists, number-list will not
% created anew but be taken from randomize-label "LABEL"
\createlistofdifferentrandomintegers{11}{1}{20}{LABEL}{\ListOfRandomNumbers}%

\ttfamily

\noindent
\string\ListOfRandomNumbers: \meaning\ListOfRandomNumbers
\bigskip

\noindent
% This calles the Macros according to the number list stored in \ListOfRandomNumbers:
\DoWithEachElementOfIntegerlist{\ListOfRandomNumbers}{\CallMacroNumber}

\end{document}

在此处输入图片描述

答案3

编码示例 2 回答https://tex.stackexchange.com/a/343436/118714

您可能希望能够单独调用每个宏:

\documentclass{article}
\usepackage{pgf}
%
\makeatletter
\newcommand\exchange[2]{#2#1}%
\newcommand\firstoftwo[2]{#1}%
\newcommand\secondoftwo[2]{#2}%
%%-------------------------------------------------------------------------
%% \Name<emptiness or tokens that are not nested in braces>{<NameOfControlSequence>}
%% yields:
%% <emptiness or tokens that are not nested in braces>\NameOfControlSequence
%%-------------------------------------------------------------------------
\newcommand\Name{}\long\def\Name#1#{\romannumeral\Innername{#1}}%
\newcommand\Innername[2]{%
  \expandafter\exchange\expandafter{\csname#2\endcsname}{0 #1}%
}%
%%======================================================================
%% Write a label expanding to a random number to the aux-file.
%% (In case the label is defined from the previous LaTeX compilation
%%  and the values for <lower bound for randomize> and <upper bound for
%%  randomize> did not change, take the value from the label stored in
%%  the .aux-file. Otherwise create a new value by means of
%%  \pgfrandominteger. In any case write that value and <lower bound for
%%  randomize> and <upper bound for randomize> to the .aux file so you
%%  can check whether something did change between the previous and the
%%  current LaTeX compilation.)
%%......................................................................
%% \RetrieveRandomfromlabel{<name of label>}%
%%                         {<macro>}%
%%                         {<lower bound>}%
%%                         {<upper bound>}%
%%                         {<amount>}%
%%                         {<action in case label is undefined or changed>}%
\newcommand\RetrieveRandomfromlabel[6]{%
  \@ifundefined{@random@#1}{\firstoftwo}{%
    \ifnum\Name
          \expandafter\expandafter
          \expandafter\firstoftwo
          \expandafter\secondoftwo{@random@#1}=#3 %
    \expandafter\firstoftwo\else\expandafter\secondoftwo\fi
    {%
      \ifnum\Name
            \expandafter\expandafter\expandafter\expandafter
            \expandafter\expandafter\expandafter\firstoftwo
            \expandafter\expandafter\expandafter\secondoftwo
            \expandafter\secondoftwo{@random@#1}=#4 %
      \expandafter\firstoftwo\else\expandafter\secondoftwo\fi
      {%
        \ifnum\Name
              \expandafter\expandafter\expandafter\expandafter
              \expandafter\expandafter\expandafter\secondoftwo
              \expandafter\expandafter\expandafter\secondoftwo
              \expandafter\secondoftwo{@random@#1}=#5 %
        \expandafter\secondoftwo\else\expandafter\firstoftwo\fi
      }{\firstoftwo}%
    }%
    {\firstoftwo}%
  }%
  {#6}%
  {%
    \expandafter\expandafter\expandafter\expandafter
    \expandafter\expandafter\expandafter\newcommand
    \expandafter\expandafter\expandafter\expandafter
    \expandafter\expandafter\expandafter#2%
    \expandafter\expandafter\expandafter\expandafter
    \expandafter\expandafter\expandafter{%
    \expandafter\expandafter\expandafter\firstoftwo
    \csname @random@#1\endcsname}%
  }%
}%
\newcommand\newrandomlabel{\@newl@bel{@random}}%
%%----------------------------------------------------------------------
%% Define a macro to expand to the value stored in a "random-label".
%%......................................................................
%% \WriteRandomMacrotolabel{<macro>}%
%%                         {<label>}%
%%                         {<lower bound>}%
%%                         {<upper bound>}%
%%                         {<amount>}%
\newcommand\WriteRandomtolabel[5]{%
  \protected@write\@auxout{}{\string\newrandomlabel{#2}{{#1}{{#3}{{#4}{#5}}}}}%
}%
%%-------------------------------------------------------------------------
%% \CreateMappingToDifferentRandomIntegers{<amount of random numbers>}%
%%                                     {<lower randomize range>}%
%%                                     {<upper randomize range>}%
%%                                     {<label prefix>}%
%%                                     {<prefix of names of mapping macros>}
\newcommand\CreateMappingToDifferentRandomIntegers[5]{%
  \@bsphack
  \ifnum#2<1 \expandafter\firstoftwo\else\expandafter\secondoftwo\fi
  {%
    \@latex@error{Lower bound is not a positive number}%
                 {Lower bound must a positive number.}%
  }{%
    \ifnum#2>#3 \expandafter\firstoftwo\else\expandafter\secondoftwo\fi
    {\@latex@error{Lower bound greater than upper bound}%
                  {Lower bound must not be greater than upper bound.}%
    }{%
      \begingroup
      \@tempcnta=#3\relax
      \advance\@tempcnta by 1%
      \advance\@tempcnta -#2\relax
      \expandafter\endgroup
      \expandafter\ifnum\the\@tempcnta<#1 %
        \expandafter\firstoftwo
      \else
        \expandafter\secondoftwo
      \fi
      {\@latex@error{Amount of elements to randomize on is
                     smaller than amount of elements to create}%
                    {Finding more unequal numbers than there are numbers
                     contained in the set from which numbers are to be
                     chosen randomly is not possible. See Dirichlet's
                     drawer principle.}%
      }{%
        \ErrorCheckingDoneCreateMappingToDifferentRandomIntegers{1}{}{}{#1}{#2}{#3}{#4}{#5}%
      }%
    }%
  }%
  \@esphack
}%
%% \ErrorCheckingDoneCreateMappingToDifferentRandomIntegers
%%   {<number K of K-th random-mapping element that is to be defined>}%
%%   {<List of random numbers>}%
%%   {<List of assignments>}% <- When this list gets carried out, random-mapping elements will get defined: \newcommand\<prefix of names of mapping macros><K>{<random number>}
%%   {<Amount of random-mapping elements that are to be defined>}%
%%   {<lower randomize range>}%
%%   {<upper randomize range>}%
%%   {<label prefix>}%
%%   {<prefix of names of mapping macros>}
%%
%%  Checks if there are already
%%  <Amount of random-mapping elements that are to be defined>
%%  assignments collected in the <List of assignments>.
%%  If so: Spit out the <List of assignments> for having them carried out.
%%  If not: Create a random number.
\newcommand\ErrorCheckingDoneCreateMappingToDifferentRandomIntegers[8]{%
  \ifnum#1>#4 %
    \expandafter\firstoftwo
  \else
    \expandafter\secondoftwo
  \fi
  {%
    #3%
    \Name\newcommand{#8}[1]{%
      \@ifundefined{#8##1}{MappingUNDefIneD}{\Name{#8##1}}%
    }%
  }{%
    \begingroup
    \CreateRandomNumbersUntilNumberIsNotInList{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}%
  }%
}%
%% \CreateRandomNumbersUntilNumberIsNotInList
%%    Repeat creating a random number until a number is found which
%%    is not already in the <List of Random Numbers>.
%%    Then
%%    - add that number to the <List of random numbers>
%%    - add an assignment-directive to the <List of assignments>
%%    - increment K
%%    - call \ErrorCheckingDoneCreateMappingToDifferentRandomIntegers.
%%
\newcommand\CreateRandomNumbersUntilNumberIsNotInList[8]{%
  \RetrieveRandomfromlabel{#7#1}{\mytempa}{#5}{#6}{#4}{%
    \pgfmathrandominteger\mytempa{#5}{#6}%
  }%
  \CheckIfNumMyTempaIsAlreadyInList#2{}{}\relax
  {%
    \CreateRandomNumbersUntilNumberIsNotInList{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}%
  }{%
     % WriteLabel #7#1 {{Wert}{{obere grenze}{{untere grenze}{menge}}}}
    \WriteRandomtolabel{\mytempa}{#7#1}{#5}{#6}{#4}%
    \@tempcnta=#1\relax
    \advance\@tempcnta 1\relax
    \expandafter\callErrorCheckingDoneCreateMappingToDifferentRandomIntegers
    \expandafter{%
    \number\expandafter\@tempcnta
           \expandafter}%
           \expandafter{%
           \mytempa}{#2}{#8#1}{#3}%
    {#4}{#5}{#6}{#7}{#8}%
  }%
}%
\newcommand\CheckIfNumMyTempaIsAlreadyInList{}%
\long\def\CheckIfNumMyTempaIsAlreadyInList#1#2\relax{%
  \ifx\relax#1\relax
    \expandafter\firstoftwo
  \else
    \expandafter\secondoftwo
  \fi
  {\secondoftwo}{%
     \ifnum\mytempa=#1 %
       \expandafter\firstoftwo
     \else
       \expandafter\secondoftwo
     \fi
     {\firstoftwo}%
     {\CheckIfNumMyTempaIsAlreadyInList#2\relax}%
  }%
}%
%
\newcommand\callErrorCheckingDoneCreateMappingToDifferentRandomIntegers[5]{%
  \endgroup
  \ErrorCheckingDoneCreateMappingToDifferentRandomIntegers{#1}%
                                              {{#2}#3}%
                                              {\Name\newcommand{#4}{#2}#5}%
}%
%
%
%
\newcommand\CallFamilysMacro[2]{%
  \Name{#1\@ifundefined{#1#2}{UNDefIneD}{#2}}%
}%
%
%
%
\newcommand\DefineInstanceOfMacroFamily[2]{%
  \Name\newcommand{#1#2}%
}%
\makeatother

\begin{document}

% Define a family of macros consisting of 20 elements plus the macros
% that are to be executed
%  - in case it is attempted to call a "family member" which is undefined
%  - in case it is attempted to map to a family member from a number where no mapping is defined.
\DefineInstanceOfMacroFamily{Macro}{UNDefIneD}{!!Macro undefined!!\\}
\DefineInstanceOfMacroFamily{Macro}{MappingUNDefIneD}{!!No Mapping defined!!\\}
\DefineInstanceOfMacroFamily{Macro}{1}{Number One\\}
\DefineInstanceOfMacroFamily{Macro}{2}{Number Two\\}
\DefineInstanceOfMacroFamily{Macro}{3}{Number Three\\}
\DefineInstanceOfMacroFamily{Macro}{4}{Number Four\\}
\DefineInstanceOfMacroFamily{Macro}{5}{Number Five\\}
\DefineInstanceOfMacroFamily{Macro}{6}{Number Six\\}
\DefineInstanceOfMacroFamily{Macro}{7}{Number Seven\\}
\DefineInstanceOfMacroFamily{Macro}{8}{Number Eight\\}
\DefineInstanceOfMacroFamily{Macro}{9}{Number Nine\\}
\DefineInstanceOfMacroFamily{Macro}{10}{Number Ten\\}
\DefineInstanceOfMacroFamily{Macro}{11}{Number Eleven\\}
\DefineInstanceOfMacroFamily{Macro}{12}{Number Twelve\\}
\DefineInstanceOfMacroFamily{Macro}{13}{Number Thirteen\\}
\DefineInstanceOfMacroFamily{Macro}{14}{Number Fourteen\\}
\DefineInstanceOfMacroFamily{Macro}{15}{Number Fifteen\\}
\DefineInstanceOfMacroFamily{Macro}{16}{Number Sixteen\\}
\DefineInstanceOfMacroFamily{Macro}{17}{Number Seventeen\\}
\DefineInstanceOfMacroFamily{Macro}{18}{Number Eighteen\\}
\DefineInstanceOfMacroFamily{Macro}{19}{Number Nineteen\\}
\DefineInstanceOfMacroFamily{Macro}{20}{Number Twenty\\}

\ttfamily

\noindent
\verb|\CallFamilysMacro{Macro}{1}|: \CallFamilysMacro{Macro}{1}%
\verb|\CallFamilysMacro{Macro}{2}|: \CallFamilysMacro{Macro}{2}%
\verb|\CallFamilysMacro{Macro}{3}|: \CallFamilysMacro{Macro}{3}%
\verb|\CallFamilysMacro{Macro}{4}|: \CallFamilysMacro{Macro}{4}%
\verb|\CallFamilysMacro{Macro}{5}|: \CallFamilysMacro{Macro}{5}%
\verb|\CallFamilysMacro{Macro}{6}|: \CallFamilysMacro{Macro}{6}%
\verb|\CallFamilysMacro{Macro}{7}|: \CallFamilysMacro{Macro}{7}%
\verb|\CallFamilysMacro{Macro}{8}|: \CallFamilysMacro{Macro}{8}%
\verb|\CallFamilysMacro{Macro}{9}|: \CallFamilysMacro{Macro}{9}%
\verb|\CallFamilysMacro{Macro}{10}|: \CallFamilysMacro{Macro}{10}%
\verb|\CallFamilysMacro{Macro}{11}|: \CallFamilysMacro{Macro}{11}%
\verb|\CallFamilysMacro{Macro}{12}|: \CallFamilysMacro{Macro}{12}%
\verb|\CallFamilysMacro{Macro}{13}|: \CallFamilysMacro{Macro}{13}%
\verb|\CallFamilysMacro{Macro}{14}|: \CallFamilysMacro{Macro}{14}%
\verb|\CallFamilysMacro{Macro}{15}|: \CallFamilysMacro{Macro}{15}%
\verb|\CallFamilysMacro{Macro}{16}|: \CallFamilysMacro{Macro}{16}%
\verb|\CallFamilysMacro{Macro}{17}|: \CallFamilysMacro{Macro}{17}%
\verb|\CallFamilysMacro{Macro}{18}|: \CallFamilysMacro{Macro}{18}%
\verb|\CallFamilysMacro{Macro}{19}|: \CallFamilysMacro{Macro}{19}%
\verb|\CallFamilysMacro{Macro}{20}|: \CallFamilysMacro{Macro}{20}%
\verb|\CallFamilysMacro{Macro}{21}|: \CallFamilysMacro{Macro}{21}% <- This yields !!Macro undefined!!
\newpage

% This defines macros
%   \LookUpInRandomMapA1 -> Random number in range 1..20
%   \LookUpInRandomMapA2 -> Another random number in range 1..20
%   ..
%   \LookUpInRandomMapA11 -> Yet another random number in range 1..20
%
%   Any two of these macros will not expand to the same number.
%
%   In the first LaTeX-run the values will be generated via
%   \pgfmathrandominteger. In consecutive LaTeX runs they will
%   be obtained from Labels "RabdomMapA1".."RabdomMapA11"
%
\CreateMappingToDifferentRandomIntegers{11}{1}{20}{RandomMapA}{LookUpInRandomMapA}%

\noindent
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{1}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{1}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{1}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{1}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{2}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{2}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{2}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{2}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{3}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{3}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{3}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{3}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{4}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{4}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{4}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{4}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{5}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{5}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{5}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{5}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{6}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{6}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{6}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{6}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{7}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{7}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{7}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{7}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{8}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{8}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{8}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{8}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{9}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{9}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{9}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{9}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{10}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{10}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{10}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{10}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{11}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{11}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{11}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{11}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{12}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{12}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{12}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{12}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{13}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{13}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{13}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{13}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{14}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{14}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{14}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{14}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{15}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{15}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{15}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{15}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{16}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{16}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{16}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{16}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{17}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{17}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{17}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{17}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{18}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{18}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{18}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{18}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{19}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{19}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{19}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{19}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{20}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{20}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{20}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{20}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{21}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{21}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapA{21}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapA{21}}%<- This yields: !!No Mapping defined!!
\newpage

% This defines macros
%   \LookUpInRandomMapB1 -> Random number in range 1..20
%   \LookUpInRandomMapB2 -> Another random number in range 1..20
%   ..
%   \LookUpInRandomMapB8 -> Yet another random number in range 1..20
%
%   Any two of these macros will not expand to the same number.
%
%   In the first LaTeX-run the values will be generated via
%   \pgfmathrandominteger. In consecutive LaTeX runs they will
%   be obtained from Labels "RabdomMapB1".."RabdomMapB8"
%
\CreateMappingToDifferentRandomIntegers{8}{1}{20}{RandoMmapB}{LookUpInRandomMapB}%

\noindent
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{1}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{1}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{1}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{1}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{2}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{2}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{2}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{2}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{3}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{3}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{3}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{3}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{4}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{4}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{4}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{4}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{5}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{5}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{5}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{5}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{6}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{6}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{6}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{6}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{7}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{7}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{7}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{7}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{8}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{8}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{8}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{8}}%
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{9}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{9}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{9}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{9}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{10}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{10}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{10}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{10}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{11}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{11}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{11}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{11}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{12}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{12}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{12}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{12}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{13}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{13}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{13}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{13}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{14}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{14}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{14}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{14}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{15}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{15}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{15}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{15}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{16}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{16}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{16}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{16}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{17}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{17}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{17}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{17}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{18}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{18}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{18}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{18}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{19}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{19}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{19}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{19}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{20}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{20}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{20}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{20}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{21}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{21}}%<- This yields: !!No Mapping defined!!
\verb|\CallFamilysMacro{Macro}{\LookUpInRandomMapB{21}}|: \CallFamilysMacro{Macro}{\LookUpInRandomMapB{21}}%<- This yields: !!No Mapping defined!!

\end{document}

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

答案4

如果我理解正确了你的最后一条评论,那么你想从 60000 个不同的元素中随机选择 30 个不同的元素,而选择它们的顺序重要吗?

可能选择的数量对应于60000 的30 个下降阶乘。即 (60000!)/((60000-30)!)。这将是一个很大的数字。

据我所知,TeX 计数寄存器中可以存储并处理的数字范围\number是从 -(2 31 -1) 到 +(2 31 -1)。


一种方法是随机选择 1 到 60000 范围内的数字,直到找到 30 个不同的数字。每次选择后,您都需要确定所选数字是否在之前的选择中已被选中。您不知道什么时候完成。如果运气不好,这个过程将花费很长时间,因为您经常会得到已经被选中的数字。当从 60000 个元素中仅选择 30 个元素时,这种情况不太可能发生,但并非完全不可能。


另一种方法可能是:

步骤A:
k:=0 ;
创建一个尚未选择的元素列表.(一开始这将是一个包含 60000 个元素的列表!!!)。

步骤B:
如果k<30 :

  • 增加  1.
  • 双射映射元素尚未选择的元素列表为 1 至 1 范围内的整数(元素数量尚未选择的元素列表)并随机找到该范围内的一个数字,并取出该范围内的元素尚未选择的元素列表映射到该数字第k  您选择的元素。然后创建一个新的尚未选择的元素列表通过从中删除选定的元素尚未选择的元素列表。然后返回步骤 B 的开始。

如果不(k < 30 ): 由于随机选择了 30 个不同的数字,因此终止。


另一种方法是创建一个由第一个  随机排列的元素n  元素,例如,通过 Fisher-Yates 洗牌算法或 Knuth 洗牌算法。
对于你的情况,你需要执行  “交流”。在  换取获得  您可以选择该子集的元素60000+1-升  元素...


另一种方法是通过以下方式创建随机排列的子集莱默解码

(n!)/((nk)!)  选择时可能的选择  不同的元素n  不同的元素,而所选元素的顺序很重要。

假设n  不同元素被双射映射到范围内的整数0..(n-1) 。

(如果元素编号从 1 到n ,您可以通过从表示所讨论元素的名义数字中减去 1 来轻松实现每个元素的这种映射。)

选择一个随机数  在范围内0..((n!/(nk)!)-1) 。 (计算该范围可能需要时间。
u⋅((nk)!)  为了莱默码(u⋅((nk)!)) th  排列n  范围内的不同数字0..(n-1)  其中排列按字典顺序排序,编号从 0 到(n!-1) 。
莱默解码 u⋅((nk)!)  以获得该排列。
莱默解码很有趣,因为它包括计算u⋅((nk)!)  在阶乘系统中,这又意味着除以范围内的所有整数1..n这需要时间. 就你的情况而言:总数=60000  ... )
仅保留第一个  从该排列中取出数字,并将它们作为所选元素映射到的数字,并通过在每个数字上加 1 来获得每个选定元素的名义数字  数字。


因为你必须处理大量的元素

  1. 使用 TeX 的计数寄存器时,数字的范围是有限的,
  2. 当不使用计数寄存器而是以可扩展的方式实现事物时,扩展会很慢——可以使用 bigintcalc 包来实现这一点,
  3. 宏参数中可放入的标记数量也是有限的(当涉及在宏参数中收集大量列表时,这一点很重要)

,无论如何我建议不要使用 (La)TeX 来实现这一点,而是编写一个外部可执行程序来计算随机选择并提供一个choice.tex包含如下序列的文本文件:

\newcommand*\RandomChoice{%
  {<Random-number1>}{<Random-number2>}{<Random-number3>}{<Random-number4>}%
  {<Random-number5>}{<Random-number6>}{<Random-number7>}{<Random-number8>}%
  {<Random-number9>}{<Random-number10>}{<Random-number11>}{<Random-number12>}%
  {<Random-number13>}{<Random-number14>}{<Random-number15>}{<Random-number16>}%
  {<Random-number17>}{<Random-number18>}{<Random-number19>}{<Random-number20>}%
  {<Random-number21>}{<Random-number22>}{<Random-number23>}{<Random-number24>}%
  {<Random-number25>}{<Random-number26>}{<Random-number27>}{<Random-number28>}%
  {<Random-number29>}{<Random-number30>}%
}

\write18如果启用了(La)TeX' 功能,则可以通过 LaTeX 运行内部调用/执行此类外部程序。

您可以使用 LaTeX2e-kernel-macro\IfFileExists\write18调用您的外部程序,从而创建文本文件(如果在调用choice.tex之前该文件尚不存在) 。\input

对于一些奇怪的项目,我确实将一个小程序的 C++ 源代码保存在一个filecontents*环境中,该环境确实会动态生成相应的 .cpp 文件。然后我用它\write18来在该 .cpp 文件上运行 C++ 编译器。然后我用它\write18来执行由 C++ 编译器生成的可执行文件。当然,这个东西不再是平台无关的了……

相关内容