![命令用于从“1,2,...,Nmax”个数字中以逗号分隔的“Nchoose”个数字列表](https://linux22.com/image/475713/%E5%91%BD%E4%BB%A4%E7%94%A8%E4%BA%8E%E4%BB%8E%E2%80%9C1%2C2%2C...%2CNmax%E2%80%9D%E4%B8%AA%E6%95%B0%E5%AD%97%E4%B8%AD%E4%BB%A5%E9%80%97%E5%8F%B7%E5%88%86%E9%9A%94%E7%9A%84%E2%80%9CNchoose%E2%80%9D%E4%B8%AA%E6%95%B0%E5%AD%97%E5%88%97%E8%A1%A8.png)
我找到了一个代码这里来自@John Kormylo,从数字\Nchoose
中选择数字1,2,...,\Nmax
并将其以逗号分隔的形式给出\mylist
。
问题: 我怎样才能命令从此,像
\createlist{<name of list>}{<Nmax>}{<Nchoose>}
例如 \createliste{pokerlist}{52}{6}
,这样我就可以说:
Show list: \pokerlist
或者Third element is: \pgfmathparse{\pokerarray[2]} \pgfmathresult
(参见“提示”)
等等。
提示1:我需要它也作为数组,但我想我可以\def\myarray{\mylist}
这么说。
提示2:这些列表中包括没有重复(例如“1,2,2,2,5,6,7,8,9,10,11,12”是不允许的。)
\documentclass{article}
\usepackage{tikz}
% Choose \Ncoose from \Nmax
\pgfmathtruncatemacro\Nmax{52}%
\pgfmathtruncatemacro\Nchoose{12}%
\begin{document}
% List of \Ncoose from \Nmax:
\foreach \i in {1,...,\Nmax} {\expandafter\xdef\csname index\i\endcsname{\i}}% create indexes
\foreach \i in {1,...,\Nmax} {% randomise (last step redundant)
\pgfmathparse{random(\i,\Nmax)}%
\let\j=\pgfmathresult
\expandafter\let\expandafter\tempa\csname index\i\endcsname
\expandafter\let\expandafter\tempb\csname index\j\endcsname
\global\expandafter\let\csname index\i\endcsname=\tempb
\global\expandafter\let\csname index\j\endcsname=\tempa
}%
\xdef\mylist{\csname index1\endcsname}% create comma delimited list
\foreach \i in {2,...,\Nchoose} {\xdef\mylist{\mylist, \csname index\i\endcsname}}%
\section{{\Nchoose} random numbers from {1,2,\dots,\Nmax}}
\noindent\mylist
\section{{33} random numbers from {1,2,\dots,60}}
...
\section{{52} random numbers from {1,2,\dots,52}}
...
\end{document}
答案1
我认为您不需要任何包代码,LaTeX 已预定义所有功能。
根据随机列表没有重复的答案
这将创建一个列表 1..n,然后对其进行打乱,并取出前 m 个条目
\documentclass{article}
\ExplSyntaxOn
\cs_generate_variant:Nn \cs_set:Npn {Npe} % old latex
\cs_generate_variant:Nn \clist_set:Nn {Ne} % old latex
\cs_new:Npn \cis_comma_tmp:n #1 {#1,}
\cs_new:Npn \cis_comma_tmpb:n #1 {\seq_item:Nn \l_tmpa_seq {#1} , }
\NewDocumentCommand\createlist{mmm}{
\clist_set:Ne \l_tmpa_clist {
\int_step_function:nN {#2} \cis_comma_tmp:n
}
\seq_set_from_clist:NN \l_tmpa_seq \l_tmpa_clist
\seq_shuffle:N \l_tmpa_seq
\cs_set:Npe#1 {
\int_step_function:nN {#3-1} \cis_comma_tmpb:n
\seq_item:Nn \l_tmpa_seq {#3}
}
}
\ExplSyntaxOff
\begin{document}
\createlist\aaa{52}{12}
\typeout{\aaa}
\createlist\bbb{60}{33}
\typeout{\bbb}
\createlist\ccc{52}{52}
\typeout{\ccc}
\end{document}
生产
12,21,36,23,2,24,3,49,25,52,8,10
9,15,36,27,51,48,10,32,55,31,49,57,47,5,42,12,29,43,17,56,44,34,35,30,2,16,41,23,28,33,37,11,59
36,34,15,49,7,45,33,31,2,29,51,50,5,10,44,17,6,43,3,14,1,19,9,23,48,24,7,32,42
,28,11,46,16,35,40,27,18,20,4,30,26,12,25,52,21,41,38,47,8,39,22,13
原始答案
这将产生以下终端输出(我留下了尾随,,
尽管它们可以被删除
40,12,45,51,43,15,6,21,8,37,16,39,
38,6,24,59,19,50,51,37,28,10,46,17,49,23,33,47,60,45,36,43,1,56,9,33,52,6,45,2,
30,2,30,49,46,
52,9,27,42,37,7,8,34,31,21,6,36,18,22,7,46,7,50,39,9,29,44,10,2,41,21,44,18,11,
20,37,16,16,36,16,26,49,7,21,18,21,40,32,28,43,31,27,25,5,18,16,6,
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand\createlist{mmm}{
\cs_set:Npe#1{\prg_replicate:nn{#3}{\int_rand:n{#2},}}}
\ExplSyntaxOff
\begin{document}
\createlist\aaa{52}{12}
\typeout{\aaa}
\createlist\bbb{60}{33}
\typeout{\bbb}
\createlist\aaa{52}{52}
\typeout{\aaa}
\end{document}
这是假设允许重复。
如果不是,则改为使用\seq_suffle
序列1...N
,然后取出第一个m
项目。
删除一个版本,
并声明e
旧乳胶版本
\documentclass{article}
\ExplSyntaxOn
\cs_generate_variant:Nn \cs_set:Npn {Npe} % old latex
\NewExpandableDocumentCommand\createlist{mmm}{
\cs_set:Npe#1{\use:e{\exp_not:N\use_none:n\use:e{\prg_replicate:nn{#3}{,\int_rand:n{#2}}}}}}
\ExplSyntaxOff
\begin{document}
\createlist\aaa{52}{12}
\typeout{\aaa}
\createlist\bbb{60}{33}
\typeout{\bbb}
\createlist\aaa{52}{52}
\typeout{\aaa}
\end{document}
答案2
如果您不想重复,则有两种策略,但 David 建议的策略要慢很多。这里我展示了两种策略:第一种是生成从 1 到 Nmax 的整数序列,然后进行打乱,并将前 Nmax 个项目存储在新序列中;第二种是生成从 1 到 Nmax 的随机整数并将其添加到序列中(如果尚未存在),当添加了 Nmax 个项目时,该过程停止。
\documentclass{article}
\usepackage{l3benchmark}
\ExplSyntaxOn
\NewDocumentCommand{\createlistA}{mmm}
{% #1 = list name, #2 = Nmax, #3 = Nchoose
\seq_clear:N \l_tmpa_seq
\int_step_inline:nn { #2 } { \seq_put_right:Nn \l_tmpa_seq { ##1 } }
\seq_shuffle:N \l_tmpa_seq
\seq_clear_new:c { l_cis_list_#1_seq }
\int_step_inline:nn { #3 }
{
\seq_put_right:ce { l_cis_list_#1_seq } { \seq_item:Nn \l_tmpa_seq { ##1 } }
}
}
\NewDocumentCommand{\createlistB}{mmm}
{% #1 = list name, #2 = Nmax, #3 = Nchoose
\seq_clear:N \l_tmpa_seq
\bool_until_do:nn { \int_compare_p:n { \seq_count:N \l_tmpa_seq = #3 } }
{
\tl_set:Ne \l_tmpa_tl { \int_rand:n { #2 } }
\seq_if_in:NVF \l_tmpa_seq \l_tmpa_tl { \seq_put_right:NV \l_tmpa_seq \l_tmpa_tl }
}
\seq_clear_new:c { l_cis_list_#1_seq }
\seq_set_eq:cN { l_cis_list_#1_seq } \l_tmpa_seq
}
\benchmark:n { \createlistA{testA}{200}{6} }
\benchmark:n { \createlistB{testB}{200}{6} }
\stop
基准测试的输出是
0.00248 seconds (1.66e4 ops)
7.69e-5 seconds (511 ops)
对于 Nmax=10 和 Nchoose=6,我们得到
1.33e-4 seconds (869 ops)
8.36e-5 seconds (556 ops)
所以当 Nmax 较小时,第二种策略也会获胜。
因此我将采用第二种策略。
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\createlist}{mmm}
{% #1 = list name, #2 = Nmax, #3 = Nchoose
\cis_createlist:nnn { #1 } { #2 } { #3 }
}
\NewExpandableDocumentCommand{\listitem}{mm}
{% #1 = list name, #2 = item number
\seq_item:cn { l_cis_list_#1_seq } { #2 }
}
\NewExpandableDocumentCommand{\printlist}{m}
{
\seq_use:cn { l_cis_list_#1_seq } { ,~ }
}
\seq_new:N \l__cis_createlist_tmp_seq
\tl_new:N \l__cis_createlist_tmp_tl
\cs_new_protected:Nn \cis_createlist:nnn
{
\seq_clear:N \l__cis_createlist_tmp_seq
\bool_until_do:nn
{
\int_compare_p:nNn { \seq_count:N \l__cis_createlist_tmp_seq } = { #3 }
}
{
\tl_set:Ne \l__cis_createlist_tmp_tl { \int_rand:n { #2 } }
\seq_if_in:NVF \l__cis_createlist_tmp_seq \l__cis_createlist_tmp_tl
{
\seq_put_right:NV \l__cis_createlist_tmp_seq \l__cis_createlist_tmp_tl
}
}
\seq_clear_new:c { l_cis_list_#1_seq }
\seq_set_eq:cN { l_cis_list_#1_seq } \l__cis_createlist_tmp_seq
}
\ExplSyntaxOff
\newcommand{\Nmax}{52}
\newcommand{\Nchoose}{12}
\begin{document}
\section{{\Nchoose} random numbers from \{1,2,\dots,\Nmax\}}
\createlist{first}{\Nmax}{\Nchoose}
\printlist{first}
\section{33 random numbers from \{1,2,\dots,60\}}
\createlist{second}{60}{33}
\printlist{second}
\section{52 random numbers from \{1,2,\dots,52\}}
\createlist{third}{52}{52}
\printlist{third}
\end{document}
如果你需要它也能与 TeX Live 2020、2021 或 2022 一起使用,请添加
\cs_generate_variant:Nn \tl_set:Nn { Ne }
后\ExplSyntaxOn
。
如果你还想支持 TeX Live 2018 或 2019,你必须
\usepackage{xparse}