supp-ran.lua /size: 4271 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
supp-ran
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to supp-ran.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
-- We cannot ask for the current seed, so we need some messy hack here.
10 11
local
report_system
=
logs
.
reporter
(
"
system
"
,
"
randomizer
"
)
12 13
local
trace_random
=
false
trackers
.
register
(
"
system.randomizer
"
,
function
(
v
)
trace_random
=
v
end
)
14
local
trace_details
=
false
trackers
.
register
(
"
system.randomizer.details
"
,
function
(
v
)
trace_details
=
v
end
)
15 16
local
insert
,
remove
=
table
.
insert
,
table
.
remove
17 18
local
math
=
math
19
local
context
=
context
20
local
implement
=
interfaces
.
implement
21 22
local
random
=
math
.
random
23
local
randomseed
=
math
.
randomseed
24
local
round
=
math
.
round
25
local
stack
=
{
}
26
local
last
=
1
27
local
maxcount
=
0x3FFFFFFF
-- 2^30-1
28 29
math
.
random
=
function
(
...
)
30
local
n
=
random
(
...
)
31
if
trace_details
then
32
report_system
(
"
math %s
"
,
n
)
33
end
34
return
n
35
end
36 37
local
function
setrandomseedi
(
n
)
38
if
n
<
=
1
then
39
n
=
n
*
maxcount
40
elseif
n
<
1000
then
41
n
=
n
*
1000
42
end
43
n
=
round
(
n
)
44
randomseed
(
n
)
45
last
=
random
(
0
,
maxcount
)
-- we need an initial value
46
if
trace_details
then
47
report_system
(
"
seed %s from %s
"
,
last
,
n
)
48
elseif
trace_random
then
49
report_system
(
"
setting seed %s
"
,
n
)
50
end
51
end
52 53
math
.
setrandomseedi
=
setrandomseedi
54 55
local
function
getrandomnumber
(
min
,
max
)
56
last
=
random
(
min
,
max
)
57
if
trace_details
then
58
report_system
(
"
number %s
"
,
last
)
59
end
60
return
last
61
end
62 63
local
function
setrandomseed
(
n
)
64
last
=
n
65
setrandomseedi
(
n
)
66
end
67 68
local
function
getrandomseed
(
)
69
return
last
70
end
71 72
-- local function getmprandomnumber()
73
-- last = random(0,4095)
74
-- if trace_details then
75
-- report_system("mp number %s",last)
76
-- end
77
-- return last
78
-- end
79 80
-- maybe stack
81 82
local
function
pushrandomseed
(
)
83
insert
(
stack
,
last
)
84
if
trace_random
or
trace_details
then
85
report_system
(
"
pushing seed %s
"
,
last
)
86
end
87
end
88 89
local
function
reuserandomseed
(
n
)
90
local
seed
=
stack
[
#
stack
]
91
if
seed
then
92
if
trace_random
or
trace_details
then
93
report_system
(
"
reusing seed %s
"
,
last
)
94
end
95
randomseed
(
seed
)
96
end
97
end
98 99
local
function
poprandomseed
(
)
100
local
seed
=
remove
(
stack
)
101
if
seed
then
102
if
trace_random
or
trace_details
then
103
report_system
(
"
popping seed %s
"
,
seed
)
104
end
105
randomseed
(
seed
)
106
end
107
end
108 109
local
function
getrandom
(
where
,
...
)
110
if
type
(
where
)
=
=
"
string
"
then
111
local
n
=
random
(
...
)
112
if
trace_details
then
113
report_system
(
"
%s %s
"
,
where
,
n
)
114
end
115
return
n
116
else
117
local
n
=
random
(
where
,
...
)
118
if
trace_details
then
119
report_system
(
"
utilities %s
"
,
n
)
120
end
121
return
n
122
end
123
end
124 125
utilities
.
randomizer
=
{
126
setseedi
=
setrandomseedi
,
127
getnumber
=
getrandomnumber
,
128
setseed
=
setrandomseed
,
129
getseed
=
getrandomseed
,
130
-- getmpnumber = getmprandomnumber,
131
pushseed
=
pushrandomseed
,
132
reuseseed
=
reuserandomseed
,
133
popseed
=
poprandomseed
,
134
get
=
getrandom
,
135
-- the original, only for testing
136
-- mathrandom = random,
137
}
138 139
-- todo: also open up in utilities.randomizer.*
140 141
implement
{
name
=
"
getrandomnumber
"
,
actions
=
{
getrandomnumber
,
context
}
,
arguments
=
{
"
integer
"
,
"
integer
"
}
}
142
implement
{
name
=
"
getrandomdimen
"
,
actions
=
{
getrandomnumber
,
context
}
,
arguments
=
{
"
dimen
"
,
"
dimen
"
}
}
143
implement
{
name
=
"
getrandomfloat
"
,
actions
=
{
getrandomnumber
,
context
}
,
arguments
=
{
"
number
"
,
"
number
"
}
}
144
--------- { name = "getmprandomnumber", actions = { getmprandomnumber, context } }
145
implement
{
name
=
"
setrandomseed
"
,
actions
=
{
setrandomseed
}
,
arguments
=
"
integer
"
}
146
implement
{
name
=
"
getrandomseed
"
,
actions
=
{
getrandomseed
,
context
}
}
147
implement
{
name
=
"
pushrandomseed
"
,
actions
=
{
pushrandomseed
}
}
148
implement
{
name
=
"
poprandomseed
"
,
actions
=
{
poprandomseed
}
}
149
implement
{
name
=
"
reuserandomseed
"
,
actions
=
{
reuserandomseed
}
}
150 151