![你能帮我制作一个随机数并通过 tikz 计算(从 1 到 n 不重复的数字)吗?](https://linux22.com/image/407650/%E4%BD%A0%E8%83%BD%E5%B8%AE%E6%88%91%E5%88%B6%E4%BD%9C%E4%B8%80%E4%B8%AA%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%B9%B6%E9%80%9A%E8%BF%87%20tikz%20%E8%AE%A1%E7%AE%97%EF%BC%88%E4%BB%8E%201%20%E5%88%B0%20n%20%E4%B8%8D%E9%87%8D%E5%A4%8D%E7%9A%84%E6%95%B0%E5%AD%97%EF%BC%89%E5%90%97%EF%BC%9F.png)
答案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}