[latex3-commits] [l3svn] r7075 - Add random-seed get/set and test if randomness is available
noreply at latex-project.org
noreply at latex-project.org
Wed Apr 12 18:16:17 CEST 2017
Author: bruno
Date: 2017-04-12 18:16:16 +0200 (Wed, 12 Apr 2017)
New Revision: 7075
Modified:
trunk/l3kernel/l3candidates.dtx
trunk/l3kernel/testfiles/m3expl001.luatex.tlg
trunk/l3kernel/testfiles/m3expl001.ptex.tlg
trunk/l3kernel/testfiles/m3expl001.tlg
trunk/l3kernel/testfiles/m3expl001.uptex.tlg
trunk/l3kernel/testfiles/m3expl001.xetex.tlg
trunk/l3kernel/testfiles/m3expl003.luatex.tlg
trunk/l3kernel/testfiles/m3expl003.ptex.tlg
trunk/l3kernel/testfiles/m3expl003.tlg
trunk/l3kernel/testfiles/m3expl003.uptex.tlg
trunk/l3kernel/testfiles/m3expl003.xetex.tlg
trunk/l3kernel/testfiles/m3rand001.lvt
trunk/l3kernel/testfiles/m3rand001.ptex.tlg
trunk/l3kernel/testfiles/m3rand001.tlg
trunk/l3kernel/testfiles/m3rand001.uptex.tlg
trunk/l3kernel/testfiles/m3rand001.xetex.tlg
Log:
Add random-seed get/set and test if randomness is available
This adds \sys_rand_seed:, \sys_gset_rand_seed: (the primitive
assigns globally) and \sys_if_rand_exist:TF. Names can be changed
Modified: trunk/l3kernel/l3candidates.dtx
===================================================================
--- trunk/l3kernel/l3candidates.dtx 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/l3candidates.dtx 2017-04-12 16:16:16 UTC (rev 7075)
@@ -473,6 +473,39 @@
% warning message of some sort.
% \end{function}
%
+% \section{Additions to \pkg{l3sys}}
+%
+% \begin{function}[added = 2017-04-12, EXP, pTF]{\sys_if_rand_exist:}
+% \begin{syntax}
+% \cs{sys_if_rand_exist_p:}
+% \cs{sys_if_rand_exist:TF} \Arg{true code} \Arg{false code}
+% \end{syntax}
+% Tests if the engine has a pseudo-random number generator. Currently
+% this is the case in \pdfTeX{} and \LuaTeX{}.
+% \end{function}
+%
+% \begin{function}[added = 2017-04-12, EXP]{\sys_rand_seed:}
+% \begin{syntax}
+% \cs{sys_rand_seed:}
+% \end{syntax}
+% Expands to the current value of the engine's random seed, a
+% non-negative integer. In engines without random number support this
+% expands to $0$.
+% \end{function}
+%
+% \begin{function}[added = 2017-04-12]{\sys_gset_rand_seed:n}
+% \begin{syntax}
+% \cs{sys_gset_rand_seed:n} \Arg{intexpr}
+% \end{syntax}
+% Sets the seed for the engine's pseudo-random number generator to the
+% \meta{integer expression}. The assignment is global. This random
+% seed affects all \cs[no-index]{\ldots{}_rand} functions (such as
+% \cs{int_rand:nn} or \cs{clist_rand_item:n}) as well as other
+% packages relying on the engine's random number generator. Currently
+% only the absolute value of the seed is used. In engines without
+% random number support this produces an error.
+% \end{function}
+%
% \section{Additions to \pkg{l3tl}}
%
% \begin{function}[EXP,pTF]{\tl_if_single_token:n}
@@ -1939,11 +1972,43 @@
}
}
% \end{macrocode}
-% \end{macro}
+% \end{macro}
%
-% \subsection{Additions to \pkg{l3tl}}
+% \subsection{Additions to \pkg{l3sys}}
%
+% \begin{macro}[EXP, pTF]{\sys_if_rand_exist:}
+% Currently, randomness exists under \pdfTeX{} and \LuaTeX{}.
% \begin{macrocode}
+\cs_if_exist:NTF \pdftex_uniformdeviate:D
+ {
+ \prg_new_conditional:Npnn \sys_if_rand_exist: { p , T , F , TF }
+ { \prg_return_true: }
+ }
+ {
+ \prg_new_conditional:Npnn \sys_if_rand_exist: { p , T , F , TF }
+ { \prg_return_false: }
+ }
+% \end{macrocode}
+% \end{macro}
+%
+% \begin{macro}[EXP]{\sys_rand_seed:}
+% Unpack the primitive.
+% \begin{macrocode}
+\cs_new:Npn \sys_rand_seed: { \tex_the:D \pdftex_randomseed:D }
+% \end{macrocode}
+% \end{macro}
+%
+% \begin{macro}{\sys_gset_rand_seed:n}
+% The primitive always assigns the seed globally.
+% \begin{macrocode}
+\cs_new_protected:Npn \sys_gset_rand_seed:n #1
+ { \pdftex_setrandomseed:D \__int_eval:w #1 \__int_eval_end: }
+% \end{macrocode}
+% \end{macro}
+%
+% \subsection{Additions to \pkg{l3tl}}
+%
+% \begin{macrocode}
%<@@=tl>
% \end{macrocode}
%
Modified: trunk/l3kernel/testfiles/m3expl001.luatex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl001.luatex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl001.luatex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4843,6 +4843,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3expl001.ptex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl001.ptex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl001.ptex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4806,6 +4806,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3expl001.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl001.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl001.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4831,6 +4831,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3expl001.uptex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl001.uptex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl001.uptex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4806,6 +4806,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3expl001.xetex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl001.xetex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl001.xetex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4816,6 +4816,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3expl003.luatex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl003.luatex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl003.luatex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4844,6 +4844,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3expl003.ptex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl003.ptex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl003.ptex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4807,6 +4807,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3expl003.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl003.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl003.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4832,6 +4832,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3expl003.uptex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl003.uptex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl003.uptex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4807,6 +4807,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3expl003.xetex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3expl003.xetex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3expl003.xetex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -4817,6 +4817,12 @@
Defining \seq_rand_item:N on line ...
Defining \seq_rand_item:c on line ...
Defining \skip_split_finite_else_action:nnNN on line ...
+Defining \sys_if_rand_exist_p: on line ...
+Defining \sys_if_rand_exist:T on line ...
+Defining \sys_if_rand_exist:F on line ...
+Defining \sys_if_rand_exist:TF on line ...
+Defining \sys_rand_seed: on line ...
+Defining \sys_gset_rand_seed:n on line ...
Defining \tl_if_single_token_p:n on line ...
Defining \tl_if_single_token:nT on line ...
Defining \tl_if_single_token:nF on line ...
Modified: trunk/l3kernel/testfiles/m3rand001.lvt
===================================================================
--- trunk/l3kernel/testfiles/m3rand001.lvt 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3rand001.lvt 2017-04-12 16:16:16 UTC (rev 7075)
@@ -1,5 +1,5 @@
%
-% Copyright (C) 2016 LaTeX3 Project
+% Copyright (C) 2016,2017 LaTeX3 Project
%
\documentclass{minimal}
@@ -12,14 +12,17 @@
\AUTHOR{Bruno Le Floch}
\ExplSyntaxOn
-\cs_if_exist_use:NT \pdftex_setrandomseed:D { 0 }
\OMIT
-\cs_if_exist:NTF \pdftex_uniformdeviate:D
- { \cs_new:Npn \test:n #1 { \prg_replicate:nn { 5 } { \fp_to_tl:n {#1} \NEWLINE } } }
- { \cs_new:Npn \test:n #1 { \fp_to_tl:n {#1} , ~ } }
-\cs_if_exist:NTF \pdftex_uniformdeviate:D
- { \cs_new:Npn \test_ii:n #1 { | \prg_replicate:nn { 5 } { #1 | } \NEWLINE } }
- { \cs_new:Npn \test_ii:n #1 { ( #1 ) ~ } }
+\sys_if_rand_exist:TF
+ {
+ \sys_gset_rand_seed:n { 0 }
+ \cs_new:Npn \test:n #1 { \prg_replicate:nn { 5 } { \fp_to_tl:n {#1} \NEWLINE } }
+ \cs_new:Npn \test_ii:n #1 { | \prg_replicate:nn { 5 } { #1 | } \NEWLINE }
+ }
+ {
+ \cs_new:Npn \test:n #1 { \fp_to_tl:n {#1} , ~ }
+ \cs_new:Npn \test_ii:n #1 { ( #1 ) ~ }
+ }
\TIMO
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -124,4 +127,25 @@
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\TEST { sys~rand~commands }
+ {
+ \sys_if_rand_exist:TF
+ {
+ \cs_set_protected:Npn \test:
+ { \TYPE { \sys_rand_seed: , \int_rand:nn { 1 } { 100 } } }
+ \sys_gset_rand_seed:n { 1 }
+ \test:
+ \group_begin:
+ \sys_gset_rand_seed:n { 0 }
+ \test:
+ \group_end:
+ \test:
+ \sys_gset_rand_seed:n { 1 }
+ \test:
+ \test:
+ }
+ { \TYPE { No~randomness } }
+ }
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\END
Modified: trunk/l3kernel/testfiles/m3rand001.ptex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3rand001.ptex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3rand001.ptex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -399,3 +399,8 @@
and I'll forget about whatever was undefined.
() ({\e }{})
============================================================
+============================================================
+TEST 9: sys rand commands
+============================================================
+No randomness
+============================================================
Modified: trunk/l3kernel/testfiles/m3rand001.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3rand001.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3rand001.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -236,3 +236,12 @@
||||||
|{\a }{\A }|{\e }{}|{\b }{{\B }}|{\b }{{\B }}|{\a }{\A }|
============================================================
+============================================================
+TEST 9: sys rand commands
+============================================================
+1,30
+0,89
+0,79
+1,30
+1,8
+============================================================
Modified: trunk/l3kernel/testfiles/m3rand001.uptex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3rand001.uptex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3rand001.uptex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -399,3 +399,8 @@
and I'll forget about whatever was undefined.
() ({\e }{})
============================================================
+============================================================
+TEST 9: sys rand commands
+============================================================
+No randomness
+============================================================
Modified: trunk/l3kernel/testfiles/m3rand001.xetex.tlg
===================================================================
--- trunk/l3kernel/testfiles/m3rand001.xetex.tlg 2017-04-12 06:13:32 UTC (rev 7074)
+++ trunk/l3kernel/testfiles/m3rand001.xetex.tlg 2017-04-12 16:16:16 UTC (rev 7075)
@@ -399,3 +399,8 @@
and I'll forget about whatever was undefined.
() ({\e }{})
============================================================
+============================================================
+TEST 9: sys rand commands
+============================================================
+No randomness
+============================================================
More information about the latex3-commits
mailing list