获取带有可选种子的随机列表

获取带有可选种子的随机列表

我想随机选择练习中的一些项目并能够以相同的顺序显示相应的解决方案。

使用xsim包时,这样的列表位于两个不同的地方,因此我考虑使用第一个列表来初始化种子,使用第二个列表来使用该种子。

我让你这个M(N)WE改编自这个回答

在此处输入图片描述

\documentclass{article}

\ExplSyntaxOn

\seq_new:N \l_randomlist_seq
\int_new:N \l_random_items_int
\NewDocumentEnvironment{randomlist}{ o  +b }
{
    % use a regular expression to split the environment
    % around the \item commands
    \regex_split:nnN { \s*\c{item}\s* } { #2 } \l_randomlist_seq
    \seq_pop_left:NN \l_randomlist_seq \l_items_tl % pop empty first item
    \seq_if_empty:NF \l_randomlist_seq {
        \seq_shuffle:N \l_randomlist_seq %% randonly shuffle items
        % the number of items to print is given by the optional #1
        % defaulting to the full list of items
        \IfNoValueTF{#1}{
            \int_set:Nn \l_random_items_int { \seq_count:N \l_randomlist_seq}
        }{
            \int_set:Nn \l_random_items_int { #1 }
        }
        % finally, insert the enumerate environment
        \begin{enumerate}
            \int_do_while:nNnn \l_random_items_int > 0 {
                \seq_pop:NN \l_randomlist_seq \l_tmpa_tl
                \item \tl_use:N \l_tmpa_tl
                \int_decr:N \l_random_items_int% one less item to go
            }
        \end{enumerate}
    }
}{}
\ExplSyntaxOff

\begin{document}
    This prints four items, it's the exercise
    \begin{randomlist}[4]   %%Generate seed
        \item A
        \item B
        \item C
        \item D
        \item E
    \end{randomlist}
    
    This prints four items it's the solutions of the 
    \begin{randomlist}[4]   %%Set seed to match the exercise seed.
        \item soltn A
        \item Soltn B
        \item Soltn C
        \item Soltn D
        \item Soltn E
    \end{randomlist}

    This prints all items
\begin{randomlist}
    \item soltn A
    \item Soltn B
    \item Soltn C
    \item Soltn D
    \item Soltn E
\end{randomlist}
\end{document}

附录:

我已尝试设置种子,但\sys_gset_rand_seed列表现在是固定的,但彼此不匹配。即,练习部分的元素与具有相同数量元素的解决方案部分的顺序不同。

\documentclass{article}

\ExplSyntaxOn

\seq_new:N \l_randomlist_seq
\int_new:N \l_random_items_int
\sys_gset_rand_seed:n {354}     %%% Despite the seed was set, the lists don't match.
\NewDocumentEnvironment{randomlist}{ o  +b }
{
    % use a regular expression to split the environment
    % around the \item commands
    \seq_set_split:Nnn \l_randomlist_seq { \item } {#2}
    \seq_pop_left:NN \l_randomlist_seq \l_items_tl % pop empty first item
    \seq_if_empty:NF \l_randomlist_seq {
        \seq_shuffle:N \l_randomlist_seq %% randonly shuffle items
        % the number of items to print is given by the optional #1
        % defaulting to the full list of items
        \IfNoValueTF{#1}{
            \int_set:Nn \l_random_items_int { \seq_count:N \l_randomlist_seq}
        }{
            \int_set:Nn \l_random_items_int { #1 }
        }
        % finally, insert the enumerate environment
        \begin{enumerate}
            \int_do_while:nNnn \l_random_items_int > 0 {
                \seq_pop:NN \l_randomlist_seq \l_tmpa_tl
                \item \tl_use:N \l_tmpa_tl
                \int_decr:N \l_random_items_int% one less item to go
            }
        \end{enumerate}
    }
}{}
\ExplSyntaxOff

\begin{document}
    This prints four items, it's the exercise
    \begin{randomlist}[4] %%Generate seed
        \item A
        \item B
        \item C
        \item D
        \item E
    \end{randomlist}
    
    This prints four items it's the solutions of the 
    \begin{randomlist}[4]   %%Set seed to match the exercise seed.
        \item Soltn A
        \item Soltn B
        \item Soltn C
        \item Soltn D
        \item Soltn E
    \end{randomlist}
    
    This prints all items in a different order than the two previous one
    \begin{randomlist}
        \item Soltn A
        \item Soltn B
        \item Soltn C
        \item Soltn D
        \item Soltn E
    \end{randomlist}
\end{document}

相关内容