你能帮我制作一个随机数并通过 tikz 计算(从 1 到 n 不重复的数字)吗?

你能帮我制作一个随机数并通过 tikz 计算(从 1 到 n 不重复的数字)吗?

我想用 tikz 对两个数字 a 和 b 进行运算(b 是 1 到 9 之间的无重复随机数)。Latex:会自行计算结果 a * b。例如: 在此处输入图片描述

答案1

我通过将数字 1 到 9 进行打乱来填充插槽,然后打印表格。

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\multiplications}{m}
 {% #1 = list of multipliers
  \nguyen_multiplications:n { #1 }
 }

% the factors to shuffle
\seq_const_from_clist:Nn \c_nguyen_multiplications_seq { 1,2,3,4,5,6,7,8,9 }
% temporary sequence
\seq_new:N \l__nguyen_multiplications_temp_seq
% the multiplications
\prop_new:N \l__nguyen_multiplications_prop
% the table body
\tl_new:N \l__nguyen_multiplications_body_tl

\cs_new_protected:Nn \nguyen_multiplications:n
 {
  \prop_clear:N \l__nguyen_multiplications_prop
  % make the columns
  \int_step_inline:nn { \clist_count:n { #1 } }
   {
    \seq_set_eq:NN \l__nguyen_multiplications_temp_seq \c_nguyen_multiplications_seq
    \seq_shuffle:N \l__nguyen_multiplications_temp_seq
    \int_step_inline:nn { 9 }
     {
      \prop_put:Nnx \l__nguyen_multiplications_prop { ##1 @ ####1 }
       { 
        { \clist_item:nn { #1 } { ##1 } }
        { \seq_item:Nn \l__nguyen_multiplications_temp_seq { ####1 } }
       }
     }
   }
  % make the table body
  \int_step_inline:nn { 9 }
   {
    \seq_clear:N \l__nguyen_multiplications_temp_seq
    \int_step_inline:nn { \clist_count:n { #1 } }
     {
      \seq_put_right:Nx \l__nguyen_multiplications_temp_seq
       {
        \__nguyen_multiplication_do:nn \prop_item:Nn \l__nguyen_multiplications_prop { ####1 @ ##1 }
       }
     }
    \tl_put_right:Nx \l__nguyen_multiplications_body_tl
     {
      \seq_use:Nn \l__nguyen_multiplications_temp_seq { & }
      \exp_not:N \\
     }
   }
   % print the table
   \group_begin:
   \setlength{\tabcolsep}{2em}
   \renewcommand{\arraystretch}{2}
   \begin{tabular}
    {
     @{}
     *{\clist_count:n { #1 }}{l}
     @{}
    }
   \tl_use:N \l__nguyen_multiplications_body_tl
   \end{tabular}
   \group_end:
 }

\cs_new_protected:Nn \__nguyen_multiplication_do:nn
 {
  $#1\times#2=\int_eval:n { (#1) * (#2) }$
 }

\ExplSyntaxOff

\begin{document}

\begin{center}
\multiplications{2,3,4}
\end{center}

\end{document}

在此处输入图片描述

对代码进行了一些小改动的变体,允许打印两次表格,一次使用框而不是结果,另一次使用数字。这在输入中通过添加尾随可选参数来指定,该参数将用于稍后再次打印表格。

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\multiplications}{mo}
 {% #1 = list of multipliers, #2 = label (optional)
  \nguyen_multiplications:nn { #1 } { #2 }
 }
\NewDocumentCommand{\showmultiplications}{m}
 {% #1 = label
  \group_begin:
  \setlength{\tabcolsep}{2em}
  \renewcommand{\arraystretch}{2}
  \tl_use:c { g_nguyen_multiplications_#1_tl }
  \group_end:
 }

% the factors to shuffle
\seq_const_from_clist:Nn \c_nguyen_multiplications_seq { 1,2,3,4,5,6,7,8,9 }
% temporary sequence
\seq_new:N \l__nguyen_multiplications_temp_seq
% the multiplications
\prop_new:N \l__nguyen_multiplications_prop
% the table body
\tl_new:N \l__nguyen_multiplications_body_tl

\cs_new_protected:Nn \nguyen_multiplications:nn
 {
  \prop_clear:N \l__nguyen_multiplications_prop
  % make the columns
  \int_step_inline:nn { \clist_count:n { #1 } }
   {
    \seq_set_eq:NN \l__nguyen_multiplications_temp_seq \c_nguyen_multiplications_seq
    \seq_shuffle:N \l__nguyen_multiplications_temp_seq
    \int_step_inline:nn { 9 }
     {
      \prop_put:Nnx \l__nguyen_multiplications_prop { ##1 @ ####1 }
       { 
        { \clist_item:nn { #1 } { ##1 } }
        { \seq_item:Nn \l__nguyen_multiplications_temp_seq { ####1 } }
       }
     }
   }
  % make the table body
  \tl_set:Nx \l__nguyen_multiplications_body_tl
   {
    \exp_not:N \begin{tabular}{@{}*{\clist_count:n{#1}}{l}@{}}
   }
  \int_step_inline:nn { 9 }
   {
    \seq_clear:N \l__nguyen_multiplications_temp_seq
    \int_step_inline:nn { \clist_count:n { #1 } }
     {
      \seq_put_right:Nx \l__nguyen_multiplications_temp_seq
       {
        \__nguyen_multiplication_do:nn \prop_item:Nn \l__nguyen_multiplications_prop { ####1 @ ##1 }
       }
     }
    \tl_put_right:Nx \l__nguyen_multiplications_body_tl
     {
      \seq_use:Nn \l__nguyen_multiplications_temp_seq { & }
      \exp_not:N \\
     }
   }
   \tl_put_right:Nn \l__nguyen_multiplications_body_tl { \end{tabular} }
   % print the table
   \group_begin:
   \setlength{\tabcolsep}{2em}
   \renewcommand{\arraystretch}{2}
   \tl_if_novalue:nF { #2 }
    {% optional argument given, save the table for later and print boxes
     \tl_new:c { g_nguyen_multiplications_#2_tl }
     \tl_gset_eq:cN { g_nguyen_multiplications_#2_tl } \l__nguyen_multiplications_body_tl
     \cs_set_eq:NN \__nguyen_multiplication_do:nn \__nguyen_multiplication_dont:nn
    }
   \tl_use:N \l__nguyen_multiplications_body_tl
   \group_end:
 }

\cs_new_protected:Nn \__nguyen_multiplication_do:nn
 {
  $#1\times#2=\int_eval:n { (#1) * (#2) }$
 }
\cs_new_protected:Nn \__nguyen_multiplication_dont:nn
 {
  $#1\times#2=\framebox{$\phantom{99}$}$
 }

\ExplSyntaxOff

\begin{document}

\section{Examples}
\begin{center}
\multiplications{1,5,9}
\end{center}

\section{Exercises}
\begin{center}
\multiplications{2,3,4}[first]
\end{center}

\section{Solutions}
\begin{center}
\showmultiplications{first}
\end{center}

\end{document}

在此处输入图片描述

答案2

我认为这就是你想要的。也许我在这里添加的函数Drop对其他应用程序有用。它用于删除从列表中随机选择的数字。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning}
\newcounter{iloop}
\makeatletter
\pgfmathdeclarefunction{Drop}{2}{%
\begingroup%
\edef\pgfmathresult{}%
\pgfutil@tempcnta0%
\@for\pgfutil@tempa:=#1\do{\unless\ifnum\pgfutil@tempa=#2\relax
\ifnum\pgfutil@tempcnta=0
\edef\pgfmathresult{\pgfutil@tempa}%
\pgfutil@tempcnta1%
\else
\edef\pgfmathresult{\pgfmathresult,\pgfutil@tempa}%
\fi
\fi}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\makeatletter

\begin{document}
\begin{tikzpicture}
\def\newlist{1,2,3,4,5,6,7,8,9}
\foreach \X in {2,3,4}
{\setcounter{iloop}{8}
\loop
\pgfmathrandominteger{\myindex}{0}{\number\value{iloop}}
\pgfmathtruncatemacro{\myinteger}{{\newlist}[\myindex]}
\pgfmathtruncatemacro{\myresult}{\myinteger*\X}
\pgfmathsetmacro{\newlist}{Drop("\newlist",\myinteger)}
\node[draw,minimum width=2em] (n-\X-\number\value{iloop}) at (2.75*\X,\number\value{iloop}) {$\myresult$};
\node[base left=0pt of n-\X-\number\value{iloop}] {$\X\times\myinteger=$};
\ifnum\value{iloop}>1
\addtocounter{iloop}{-1}%
\repeat
\addtocounter{iloop}{-1}%
\pgfmathtruncatemacro{\myinteger}{\newlist}
\pgfmathtruncatemacro{\myresult}{\myinteger*\X}
\node[draw,minimum width=2em] (n-\X-\number\value{iloop}) at (2.75*\X,\number\value{iloop}) {$\myresult$};
\node[base left=0pt of n-\X-\number\value{iloop}] {$\X\times\myinteger=$};
}
\end{tikzpicture}    
\end{document}

在此处输入图片描述

答案3

这是一个简短的代码,它不能产生真正的随机洗牌,但它可能对你来说足够了。

\documentclass{article}
\usepackage{pgffor}
\usepackage{multicol}

\begin{document}
  \begin{multicols}{3}
    \foreach \x in {2,3,4}{
      % let's shuffle y's
      \xdef\ys{1}
      \foreach[evaluate={\r=int(2*int(rnd*2))}] \y in {2,...,9}{
        \ifnum \r <1 \xdef\ys{\y,\ys} \else \xdef\ys{\ys,\y} \fi
      }
      % use shuffled y's
      \foreach[evaluate={\z=int(\y*\x)}] \y in \ys{
        \noindent $\x \times \y = \framebox[7mm]{\hfill\z}$\\
      }

      \columnbreak
    }
  \end{multicols}
\end{document}

在此处输入图片描述

相关内容