| 1 |
% $Id: luarandom.sty 813 2023-11-13 19:33:29Z herbert $ |
| 2 |
%% |
| 3 |
%% This is file `luarandom.sty'. |
| 4 |
%% |
| 5 |
%% IMPORTANT NOTICE: |
| 6 |
%% |
| 7 |
%% luarandom Copyright (C) 2018,2023- Herbert Voss <[email protected]> |
| 8 |
%% |
| 9 |
%% This package may be distributed under the terms of the LaTeX Project |
| 10 |
%% Public License, as described in lppl.txt in the base LaTeX distribution. |
| 11 |
%% Either version 1.3 or, at your option, any later version. |
| 12 |
%% |
| 13 |
|
| 14 |
\NeedsTeXFormat{LaTeX2e} |
| 15 |
\ProvidesPackage{luarandom}[2023/11/12 v 0.02 package for random numbers] |
| 16 |
\RequirePackage{iftex} |
| 17 |
|
| 18 |
\def\lua@nl{^^J\space\space\space\space} |
| 19 |
\newcommand\lua@PackageError[2]{\PackageError{luarandom}{\lua@nl #1^^J}{#2}} |
| 20 |
\ifluatex\else |
| 21 |
\lua@PackageError{% |
| 22 |
"You are not using LuaTeX\app@nl |
| 23 |
the lua definitions will not be available!} |
| 24 |
{If you run the source from a GUI then set |
| 25 |
the compiler "lualatex" in the |
| 26 |
preferences.}% |
| 27 |
\fi |
| 28 |
|
| 29 |
\RequirePackage{luacode} |
| 30 |
\begin{luacode*} |
| 31 |
RandomNumbers = {} |
| 32 |
|
| 33 |
function allFound(R) |
| 34 |
local r1 = R[1] |
| 35 |
local i |
| 36 |
for i=2,#R do |
| 37 |
r1 = r1 and R[i] |
| 38 |
if not r1 then return false end |
| 39 |
end |
| 40 |
return true |
| 41 |
end |
| 42 |
|
| 43 |
function makeRandomNumberList(l,r,n,seed) |
| 44 |
RandomNumbers = {} |
| 45 |
if seed == 0 then |
| 46 |
math.randomseed(os.time()) |
| 47 |
else |
| 48 |
math.randomseed(seed) |
| 49 |
end |
| 50 |
local R = {} |
| 51 |
local i,j |
| 52 |
for i=1,n do R[i] = false end |
| 53 |
repeat |
| 54 |
local rand = math.random(l,r) |
| 55 |
if not R[rand+1-l] then |
| 56 |
R[rand+1-l] = true |
| 57 |
RandomNumbers[#RandomNumbers+1] = rand |
| 58 |
end |
| 59 |
until allFound(R) |
| 60 |
end |
| 61 |
|
| 62 |
function makeSimpleRandomNumberList(l,r,n,seed) |
| 63 |
RandomNumbers = {} |
| 64 |
if seed == 0 then |
| 65 |
math.randomseed(os.time()/3) |
| 66 |
else |
| 67 |
math.randomseed(seed) |
| 68 |
end |
| 69 |
local i |
| 70 |
for i=1,n do RandomNumbers[#RandomNumbers+1] = math.random(l,r) end |
| 71 |
end |
| 72 |
|
| 73 |
function getRand(i) |
| 74 |
tex.print(RandomNumbers[i]) |
| 75 |
end |
| 76 |
\end{luacode*} |
| 77 |
|
| 78 |
\newcommand\makeRandomNumberList[4][0]{% |
| 79 |
\directlua{makeRandomNumberList(#2,#3,#4,#1)}} |
| 80 |
\newcommand\makeSimpleRandomNumberList[4][0]{% |
| 81 |
\directlua{makeSimpleRandomNumberList(#2,#3,#4,#1)}} |
| 82 |
\def\getNumberFromList#1{\directlua{getRand(#1)}} |
| 83 |
|
| 84 |
|
| 85 |
\endinput |