![关于伪随机数生成器](https://linux22.com/image/387553/%E5%85%B3%E4%BA%8E%E4%BC%AA%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%94%9F%E6%88%90%E5%99%A8.png)
Schulte Table(Tran Le Nam 的作者)的 TikZ 代码:
\documentclass[12pt,tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\usepackage{xstring}
\begin{document}
\large
\begin{tikzpicture}
\def\n{5}
\def\mylist{}
\pgfmathsetmacro{\numbermax}{\n*\n}
\foreach \x in {1,...,\numbermax}{
\xdef\mylist{\mylist\noexpand{\x\noexpand}}
}
\begin{scope}
\pgfmathsetmacro{\nt}{\n-1}
\foreach \i in {0,...,\nt}
{
\foreach \j in {0,...,\nt}
{ \pgfmathdeclarerandomlist{mynum}{\mylist}
\pgfmathrandomitem{\mynum}{mynum}
\StrSubstitute{\mylist}{{\mynum}}{}[\sublist]
\global\let\mylist\sublist
\pgfmathparse{0.9*rnd+0.3} \pgfmathsetmacro{\maum}{\pgfmathresult}
\pgfmathparse{0.8*rnd} \pgfmathsetmacro{\mauh}{\pgfmathresult}
\definecolor{mau}{rgb}{\maum,\mauh,0.1}
\node[color=mau] at ($ (\i+0.5, \j+0.5 ) $) {\mynum};
}
}
\draw (0,0) grid (\n,\n);
\end{scope}
\end{tikzpicture}
\end{document}
说实话,到现在为止,我还不知道任何 PSTricks 文档中有“随机语法”一说。我能在哪里找到“随机语法”?
更新:
\documentclass[12pt,pstricks,border=5pt]{standalone}
\usepackage{pst-math}
\usepackage{multido}
\begin{document}
\newcounter{RandNo}
\setcounter{RandNo}{1}
\def\n{7} \def\m{10}
\def\N{\the\numexpr\m*\n}
\defineRandIntervall(1,\N){\N}
\makeRandomNumberList
%\resetcolorseries[\N]{test}
\begin{pspicture}(\m,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\multido{\rRow=0.5+1.0}{\n}{\multido{\rCol=0.5+1.0}{\m}{%
\rput(\rCol,\rRow){\getNumberFromList{\theRandNo}}%
\stepcounter{RandNo}}}
\end{pspicture}
\end{document}
它正常产生图形:
但
\documentclass[12pt,pstricks,border=5pt]{standalone}
\usepackage{pst-math}
\usepackage{multido}
\definecolorseries{test}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
\begin{document}
\newcounter{RandNo}
\setcounter{RandNo}{1}
\def\n{7} \def\m{10}
\def\N{\the\numexpr\m*\n}
\defineRandIntervall(1,\N){\N}
\makeRandomNumberList
\resetcolorseries[\N]{test}
\begin{pspicture}(\m,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\multido{\rRow=0.5+1.0}{\n}{\multido{\rCol=0.5+1.0}{\m}{%
\rput(\rCol,\rRow){\textcolor{test!!+}{\getNumberFromList{\theRandNo}}%
\stepcounter{RandNo}}}
\end{pspicture}
\end{document}
编译器没有得到结果.... :((
答案1
做不是与之一起工作xelatex
(它不知道随机函数)
\documentclass[12pt]{article}
\usepackage{pstricks,pst-calculate,multido}
\usepackage{auto-pst-pdf}
\definecolorseries{test}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
\begin{document}
\def\n{5}
\begin{pspicture}(\n,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\resetcolorseries[25]{test}
\multido{\rRow=0.5+1.0}{\n}{%
\multido{\rCol=0.5+1.0}{\n}{%
\rput(\rCol,\rRow){\textcolor{test!!+}{\pscalculate{randint(1,25)}}}
}}
\end{pspicture}
\end{document}
还有一个带有单个随机数的解决方案。随机内容是最新的一部分pst-math
:
\documentclass[12pt]{article}
\usepackage{pstricks,pst-math}
\usepackage{multido}
\usepackage{auto-pst-pdf}
\definecolorseries{test}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
\begin{document}
\newcounter{RandNo} \setcounter{RandNo}{1}
\def\n{5} \def\N{\the\numexpr\n*\n}
\defineRandIntervall(1,\N){\N}
\makeRandomNumberList
\resetcolorseries[25]{test}
\begin{pspicture}(\n,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\multido{\rRow=0.5+1.0}{\n}{\multido{\rCol=0.5+1.0}{\n}{%
\rput(\rCol,\rRow){\textcolor{test!!+}{\getNumberFromList{\theRandNo}}}%
\stepcounter{RandNo}}}
\end{pspicture}
\end{document}
luatex
对于大正方形,使用它进行计算更有意义。使用 运行以下示例lualatex --shell-escape <file>
需要包luarandom
,该包将在未来几天内为 TeXLive 或 MikTeX 提供。或者在这里: http://comedy.dante.de/~herbert/TeXnik/tex/lualatex/luarandom/
\documentclass[12pt]{article}
\usepackage{xcolor,pstricks}
\usepackage{multido}
\usepackage{pst-math}
\usepackage{auto-pst-pdf-lua}
\ifpdf\else\usepackage[T1]{fontenc}\fi
\begin{document}
\newcounter{RandNo} \setcounter{RandNo}{1}
\def\n{10} \def\N{\the\numexpr\n*\n}
\makeRandomNumberList{1}{\N}{\N}
\begin{pspicture}(\n,\n)
\psgrid[subgriddiv=0,gridlabels=0pt]
\multido{\rRow=0.5+1.0}{\n}{\multido{\rCol=0.5+1.0}{\n}{%
\edef\RNumber{\getNumberFromList{\theRandNo}}%
\rput(\rCol,\rRow){\textcolor{randomhsb}{\RNumber}%
\stepcounter{RandNo}}}}
\end{pspicture}
\end{document}
答案2
纯粹的 PSTricks 解决方案也是可行的,但我们不应该重新发明轮子。
\documentclass[12pt,pstricks,border=1pt]{standalone}
\usepackage{tikz,xstring}
\usetikzlibrary{calc}
\def\n{5}\def\mylist{}
\pgfmathsetmacro{\numbermax}{\n*\n}
\foreach \x in {1,...,\numbermax}{
\xdef\mylist{\mylist\noexpand{\x\noexpand}}
}
\pgfmathsetmacro{\nt}{\n-1}
\newpsstyle{gridstyle}{griddots=0,subgriddiv=1}
\begin{document}
\large
\begin{pspicture}[showgrid](\n,\n)
\foreach \i in {0,...,\nt}
{
\foreach \j in {0,...,\nt}
{ \pgfmathdeclarerandomlist{mynum}{\mylist}
\pgfmathrandomitem{\mynum}{mynum}
\StrSubstitute{\mylist}{{\mynum}}{}[\sublist]
\global\let\mylist\sublist
\pgfmathparse{0.9*rnd+0.3} \pgfmathsetmacro{\maum}{\pgfmathresult}
\pgfmathparse{0.8*rnd} \pgfmathsetmacro{\mauh}{\pgfmathresult}
\definecolor{mau}{rgb}{\maum,\mauh,0.1}
\rput(!\i\space .5 add \j\space .5 add){\color{mau}\mynum}
}
}
\end{pspicture}
\end{document}
关于伪随机数生成器
Rand
产生一个介于 0 和 1 之间(包括 0 和 1)的浮点随机数,并rand
产生一个“巨大”的整数。
\documentclass[pstricks,border=12pt,12pt]{standalone}
\pstVerb{realtime srand}
\begin{document}
\begin{pspicture}[showgrid](6,6)
\psLoop{5}{\qdisk(!Rand 6 mul Rand 6 mul){!Rand 4 div}}
\end{pspicture}
\end{document}