[latex3-commits] [git/LaTeX3-latex3-latex3] master: Implement candidate \int_rand:n (8e74155)

Bruno Le Floch bruno at le-floch.fr
Sun May 6 16:13:51 CEST 2018


Repository : https://github.com/latex3/latex3
On branch  : master
Link       : https://github.com/latex3/latex3/commit/8e74155c3b314bdda0e60e2c831e7af8a8880c9c

>---------------------------------------------------------------

commit 8e74155c3b314bdda0e60e2c831e7af8a8880c9c
Author: Bruno Le Floch <bruno at le-floch.fr>
Date:   Sat May 5 19:41:48 2018 -0400

    Implement candidate \int_rand:n
    
    Only one arg, in analogy with \int_step_function:nN.  Besides, this
    \int_rand:n is faster than \int_rand:nn { 1 }.


>---------------------------------------------------------------

8e74155c3b314bdda0e60e2c831e7af8a8880c9c
 l3kernel/l3candidates.dtx               |   11 ++++++++
 l3kernel/l3fp-random.dtx                |   31 +++++++++++++++++++++++
 l3kernel/testfiles/m3expl001.luatex.tlg |    2 ++
 l3kernel/testfiles/m3expl001.ptex.tlg   |    2 ++
 l3kernel/testfiles/m3expl001.tlg        |    2 ++
 l3kernel/testfiles/m3expl001.uptex.tlg  |    2 ++
 l3kernel/testfiles/m3expl001.xetex.tlg  |    1 +
 l3kernel/testfiles/m3expl003.luatex.tlg |    2 ++
 l3kernel/testfiles/m3expl003.ptex.tlg   |    2 ++
 l3kernel/testfiles/m3expl003.tlg        |    2 ++
 l3kernel/testfiles/m3expl003.uptex.tlg  |    2 ++
 l3kernel/testfiles/m3expl003.xetex.tlg  |    1 +
 l3kernel/testfiles/m3rand001.lvt        |    9 +++++++
 l3kernel/testfiles/m3rand001.tlg        |   17 +++++++++++++
 l3kernel/testfiles/m3rand001.xetex.tlg  |   42 +++++++++++++++++++++++++++++++
 15 files changed, 128 insertions(+)

diff --git a/l3kernel/l3candidates.dtx b/l3kernel/l3candidates.dtx
index 74a7203..24e62b9 100644
--- a/l3kernel/l3candidates.dtx
+++ b/l3kernel/l3candidates.dtx
@@ -423,6 +423,17 @@
 %   locally.
 % \end{function}
 %
+% \section{Additions to \pkg{l3int}}
+%
+% \begin{function}[EXP, added = 2018-05-05]{\int_rand:n}
+%   \begin{syntax}
+%     \cs{int_rand:n} \Arg{intexpr}
+%   \end{syntax}
+%   Evaluates the \meta{integer expression} then produces a
+%   pseudo-random number between $1$ and the \meta{intexpr} (included).
+%   This is not yet available in \XeTeX{}.
+% \end{function}
+%
 % \section{Additions to \pkg{l3intarray}}
 %
 % \subsection{Working with contents of integer arrays}
diff --git a/l3kernel/l3fp-random.dtx b/l3kernel/l3fp-random.dtx
index d259099..ecd74de 100644
--- a/l3kernel/l3fp-random.dtx
+++ b/l3kernel/l3fp-random.dtx
@@ -91,6 +91,12 @@
           { \int_rand:nn {#1} {#2} }
         \int_eval:n {#1}
       }
+    \cs_new:Npn \int_rand:n #1
+      {
+        \__kernel_msg_expandable_error:nnn { kernel } { fp-no-random }
+          { \int_rand:n {#1} }
+        1
+      }
   }
 \sys_if_rand_exist:T
   {
@@ -486,6 +492,31 @@
 %    \end{macrocode}
 % \end{macro}
 %
+% \begin{macro}{\int_rand:n, \@@_randint:n}
+%   Similar to \cs{int_rand:nn}, but needs fewer checks.
+%    \begin{macrocode}
+    \cs_new:Npn \int_rand:n #1
+      {
+        \int_eval:n
+          { \exp_args:Nf \@@_randint:n { \int_eval:n {#1} } }
+      }
+    \cs_new:Npn \@@_randint:n #1
+      {
+        \if_int_compare:w #1 < 1 \exp_stop_f:
+          \__kernel_msg_expandable_error:nnnn
+            { kernel } { randint-backward-range } { 1 } {#1}
+          \@@_randint:ww #1; 1;
+        \else:
+          \if_int_compare:w #1 > \c_@@_rand_critical_int
+            \fp_to_int:n { randint(#1) }
+          \else:
+            \@@_randint:nn {1} {#1}
+          \fi:
+        \fi:
+      }
+%    \end{macrocode}
+% \end{macro}
+%
 % End the initial conditional that ensures these commands are only
 % defined in engines that support random numbers.
 %    \begin{macrocode}
diff --git a/l3kernel/testfiles/m3expl001.luatex.tlg b/l3kernel/testfiles/m3expl001.luatex.tlg
index 8c694e9..41ec364 100644
--- a/l3kernel/testfiles/m3expl001.luatex.tlg
+++ b/l3kernel/testfiles/m3expl001.luatex.tlg
@@ -4242,6 +4242,8 @@ Defining \__fp_randint_auxiv_o:ww on line ...
 Defining \__fp_randint_auxv_o:w on line ...
 Defining \int_rand:nn on line ...
 Defining \__fp_randint:ww on line ...
+Defining \int_rand:n on line ...
+Defining \__fp_randint:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3expl001.ptex.tlg b/l3kernel/testfiles/m3expl001.ptex.tlg
index ddc2899..875492a 100644
--- a/l3kernel/testfiles/m3expl001.ptex.tlg
+++ b/l3kernel/testfiles/m3expl001.ptex.tlg
@@ -4497,6 +4497,8 @@ Defining \__fp_randint_auxiv_o:ww on line ...
 Defining \__fp_randint_auxv_o:w on line ...
 Defining \int_rand:nn on line ...
 Defining \__fp_randint:ww on line ...
+Defining \int_rand:n on line ...
+Defining \__fp_randint:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3expl001.tlg b/l3kernel/testfiles/m3expl001.tlg
index 0853d68..a3e2c94 100644
--- a/l3kernel/testfiles/m3expl001.tlg
+++ b/l3kernel/testfiles/m3expl001.tlg
@@ -4497,6 +4497,8 @@ Defining \__fp_randint_auxiv_o:ww on line ...
 Defining \__fp_randint_auxv_o:w on line ...
 Defining \int_rand:nn on line ...
 Defining \__fp_randint:ww on line ...
+Defining \int_rand:n on line ...
+Defining \__fp_randint:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3expl001.uptex.tlg b/l3kernel/testfiles/m3expl001.uptex.tlg
index 34cce43..992f14a 100644
--- a/l3kernel/testfiles/m3expl001.uptex.tlg
+++ b/l3kernel/testfiles/m3expl001.uptex.tlg
@@ -4496,6 +4496,8 @@ Defining \__fp_randint_auxiv_o:ww on line ...
 Defining \__fp_randint_auxv_o:w on line ...
 Defining \int_rand:nn on line ...
 Defining \__fp_randint:ww on line ...
+Defining \int_rand:n on line ...
+Defining \__fp_randint:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3expl001.xetex.tlg b/l3kernel/testfiles/m3expl001.xetex.tlg
index ba2a823..5d50ab1 100644
--- a/l3kernel/testfiles/m3expl001.xetex.tlg
+++ b/l3kernel/testfiles/m3expl001.xetex.tlg
@@ -4227,6 +4227,7 @@ Defining message LaTeX/kernel/fp-no-random on line ...
 Defining \__fp_rand_o:Nw on line ...
 Defining \__fp_randint_o:Nw on line ...
 Defining \int_rand:nn on line ...
+Defining \int_rand:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.luatex.tlg b/l3kernel/testfiles/m3expl003.luatex.tlg
index 8c694e9..41ec364 100644
--- a/l3kernel/testfiles/m3expl003.luatex.tlg
+++ b/l3kernel/testfiles/m3expl003.luatex.tlg
@@ -4242,6 +4242,8 @@ Defining \__fp_randint_auxiv_o:ww on line ...
 Defining \__fp_randint_auxv_o:w on line ...
 Defining \int_rand:nn on line ...
 Defining \__fp_randint:ww on line ...
+Defining \int_rand:n on line ...
+Defining \__fp_randint:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.ptex.tlg b/l3kernel/testfiles/m3expl003.ptex.tlg
index ddc2899..875492a 100644
--- a/l3kernel/testfiles/m3expl003.ptex.tlg
+++ b/l3kernel/testfiles/m3expl003.ptex.tlg
@@ -4497,6 +4497,8 @@ Defining \__fp_randint_auxiv_o:ww on line ...
 Defining \__fp_randint_auxv_o:w on line ...
 Defining \int_rand:nn on line ...
 Defining \__fp_randint:ww on line ...
+Defining \int_rand:n on line ...
+Defining \__fp_randint:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.tlg b/l3kernel/testfiles/m3expl003.tlg
index 0853d68..a3e2c94 100644
--- a/l3kernel/testfiles/m3expl003.tlg
+++ b/l3kernel/testfiles/m3expl003.tlg
@@ -4497,6 +4497,8 @@ Defining \__fp_randint_auxiv_o:ww on line ...
 Defining \__fp_randint_auxv_o:w on line ...
 Defining \int_rand:nn on line ...
 Defining \__fp_randint:ww on line ...
+Defining \int_rand:n on line ...
+Defining \__fp_randint:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.uptex.tlg b/l3kernel/testfiles/m3expl003.uptex.tlg
index 34cce43..992f14a 100644
--- a/l3kernel/testfiles/m3expl003.uptex.tlg
+++ b/l3kernel/testfiles/m3expl003.uptex.tlg
@@ -4496,6 +4496,8 @@ Defining \__fp_randint_auxiv_o:ww on line ...
 Defining \__fp_randint_auxv_o:w on line ...
 Defining \int_rand:nn on line ...
 Defining \__fp_randint:ww on line ...
+Defining \int_rand:n on line ...
+Defining \__fp_randint:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.xetex.tlg b/l3kernel/testfiles/m3expl003.xetex.tlg
index ba2a823..5d50ab1 100644
--- a/l3kernel/testfiles/m3expl003.xetex.tlg
+++ b/l3kernel/testfiles/m3expl003.xetex.tlg
@@ -4227,6 +4227,7 @@ Defining message LaTeX/kernel/fp-no-random on line ...
 Defining \__fp_rand_o:Nw on line ...
 Defining \__fp_randint_o:Nw on line ...
 Defining \int_rand:nn on line ...
+Defining \int_rand:n on line ...
 Defining \g__fp_array_int on line ...
 \g__fp_array_int=\count...
 Defining \l__fp_array_loop_int on line ...
diff --git a/l3kernel/testfiles/m3rand001.lvt b/l3kernel/testfiles/m3rand001.lvt
index 73f163c..ac7dcdb 100644
--- a/l3kernel/testfiles/m3rand001.lvt
+++ b/l3kernel/testfiles/m3rand001.lvt
@@ -151,4 +151,13 @@
   }
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\TESTEXP { Random~integer~in~a~range }
+  {
+    \int_rand:n { -1 } \NEWLINE
+    \test_ii:n { \int_rand:n { \c_max_int } }
+    \test_ii:n { \int_rand:n { 1 } }
+    \test_ii:n { \int_rand:n { 11 } }
+  }
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \END
diff --git a/l3kernel/testfiles/m3rand001.tlg b/l3kernel/testfiles/m3rand001.tlg
index 138d849..39cae1f 100644
--- a/l3kernel/testfiles/m3rand001.tlg
+++ b/l3kernel/testfiles/m3rand001.tlg
@@ -275,3 +275,20 @@ TEST 9: sys rand commands
 1,71
 1,55
 ============================================================
+============================================================
+TEST 10: Random integer in a range
+============================================================
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Bounds ordered backwards in \int_rand:nn {1} {-1}.
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+0
+|1226983952|2137676231|708840819|1697269430|585282601|1640562914|568795804|124937418|1811019699|262907525|625094442|1065693082|1052955123|921192786|389984657|
+|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|
+|2|10|7|9|6|11|10|6|6|2|4|1|3|1|2|
+============================================================
diff --git a/l3kernel/testfiles/m3rand001.xetex.tlg b/l3kernel/testfiles/m3rand001.xetex.tlg
index bd6b4f0..04140fc 100644
--- a/l3kernel/testfiles/m3rand001.xetex.tlg
+++ b/l3kernel/testfiles/m3rand001.xetex.tlg
@@ -434,3 +434,45 @@ TEST 9: sys rand commands
 ============================================================
 No randomness
 ============================================================
+============================================================
+TEST 10: Random integer in a range
+============================================================
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Random numbers unavailable for \int_rand:n {-1}
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Random numbers unavailable for \int_rand:n {\c_ma...
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Random numbers unavailable for \int_rand:n {1}
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Random numbers unavailable for \int_rand:n {11}
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+1
+(1) (1) (1) 
+============================================================





More information about the latex3-commits mailing list