util-ran.lua /size: 2515 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['util-ran'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.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
9local random  = math.random
10local concat = table.concat
11local sub, upper = string.sub, string.upper
12
13local randomizers     = utilities.randomizers or { }
14utilities.randomizers = randomizers
15
16local l_one = "bcdfghjklmnpqrstvwxz"
17local l_two = "aeiouy"
18
19local u_one = upper(l_one)
20local u_two = upper(l_two)
21
22local n_one = #l_one
23local n_two = #l_two
24
25function randomizers.word(min,max,separator)
26    local t = { }
27    for i=1,random(min,max) do
28        if i % 2 == 0 then
29            local r = random(1,n_one)
30            t[i] = sub(l_one,r,r)
31        else
32            local r = random(1,n_two)
33            t[i] = sub(l_two,r,r)
34        end
35    end
36    return concat(t,separator)
37end
38
39function randomizers.initials(min,max)
40    if not min then
41        if not max then
42            min, max = 1, 3
43        else
44            min, max = 1, min
45        end
46    elseif not max then
47        max = min
48    end
49    local t = { }
50    local n = random(min or 1,max or 3)
51    local m = 0
52    for i=1,n do
53        m = m + 1
54        if i % 2 == 0 then
55            local r = random(1,n_one)
56            t[m] = sub(u_one,r,r)
57        else
58            local r = random(1,n_two)
59            t[m] = sub(u_two,r,r)
60        end
61        m = m + 1
62        t[m] = "."
63    end
64    return concat(t)
65end
66
67function randomizers.firstname(min,max)
68    if not min then
69        if not max then
70            min, max = 3, 10
71        else
72            min, max = 1, min
73        end
74    elseif not max then
75        max = min
76    end
77    local t = { }
78    local n = random(min,max)
79    local b = true
80    if n % 2 == 0 then
81        local r = random(1,n_two)
82        t[1] = sub(u_two,r,r)
83        b = true
84    else
85        local r = random(1,n_one)
86        t[1] = sub(u_one,r,r)
87        b = false
88    end
89    for i=2,n do
90        if b then
91            local r = random(1,n_one)
92            t[i] = sub(l_one,r,r)
93            b = false
94        else
95            local r = random(1,n_two)
96            t[i] = sub(l_two,r,r)
97            b = true
98        end
99    end
100    return concat(t,separator)
101end
102
103randomizers.surname = randomizers.firstname
104
105-- for i=1,10 do
106--     print(randomizers.initials(1,3),randomizers.firstname(5,10),randomizers.surname(5,15))
107-- end
108