summaryrefslogtreecommitdiff
path: root/tex/context/base/util-ran.lua
blob: 50d0a7082ac2c6875724315a4e63991385fb9cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
if not modules then modules = { } end modules ['util-ran'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local random  = math.random
local concat = table.concat
local sub, upper = string.sub, string.upper

local randomizers     = utilities.randomizers or { }
utilities.randomizers = randomizers

local l_one = "bcdfghjklmnpqrstvwxz"
local l_two = "aeiouy"

local u_one = upper(l_one)
local u_two = upper(l_two)

local n_one = #l_one
local n_two = #l_two

function randomizers.word(min,max,separator)
    local t = { }
    for i=1,random(min,max) do
        if i % 2 == 0 then
            local r = random(1,n_one)
            t[i] = sub(l_one,r,r)
        else
            local r = random(1,n_two)
            t[i] = sub(l_two,r,r)
        end
    end
    return concat(t,separator)
end

function randomizers.initials(min,max)
    if not min then
        if not max then
            min, max = 1, 3
        else
            min, max = 1, min
        end
    elseif not max then
        max = min
    end
    local t = { }
    local n = random(min or 1,max or 3)
    local m = 0
    for i=1,n do
        m = m + 1
        if i % 2 == 0 then
            local r = random(1,n_one)
            t[m] = sub(u_one,r,r)
        else
            local r = random(1,n_two)
            t[m] = sub(u_two,r,r)
        end
        m = m + 1
        t[m] = "."
    end
    return concat(t)
end

function randomizers.firstname(min,max)
    if not min then
        if not max then
            min, max = 3, 10
        else
            min, max = 1, min
        end
    elseif not max then
        max = min
    end
    local t = { }
    local n = random(min,max)
    local b = true
    if n % 2 == 0 then
        local r = random(1,n_two)
        t[1] = sub(u_two,r,r)
        b = true
    else
        local r = random(1,n_one)
        t[1] = sub(u_one,r,r)
        b = false
    end
    for i=2,n do
        if b then
            local r = random(1,n_one)
            t[i] = sub(l_one,r,r)
            b = false
        else
            local r = random(1,n_two)
            t[i] = sub(l_two,r,r)
            b = true
        end
    end
    return concat(t,separator)
end

randomizers.surname = randomizers.firstname

-- for i=1,10 do
--     print(randomizers.initials(1,3),randomizers.firstname(5,10),randomizers.surname(5,15))
-- end