-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrandom.sql
More file actions
168 lines (153 loc) · 4.24 KB
/
random.sql
File metadata and controls
168 lines (153 loc) · 4.24 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Demonstration Database Generator
Copyright (c) 2025 Postgres Professional
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
--
-- Random numbers generation
--
/*
Uniform distribution of timestamps.
*/
CREATE OR REPLACE FUNCTION rnd_uniform(
ts_from timestamptz,
ts_to timestamptz,
round_to integer DEFAULT 5 -- round off to minutes
)
RETURNS timestamptz
AS $$
WITH unrounded as (
SELECT ts_from + (ts_to - ts_from) * random() as d
)
SELECT make_timestamptz(
extract(year from d)::integer,
extract(month from d)::integer,
extract(day from d)::integer,
extract(hour from d)::integer,
(trunc(extract(minute from d) / round_to))::integer * round_to,
0.0,
to_char(extract(timezone from d)/3600,'SG00')
)
FROM unrounded;
$$ LANGUAGE sql;
/*
Uniform distribution of times.
*/
CREATE OR REPLACE FUNCTION rnd_uniform(
t_from time,
t_to time,
round_to integer DEFAULT 5 -- round off to minutes
)
RETURNS time
AS $$
WITH unrounded as (
SELECT t_from + (t_to - t_from) * random() as d
)
SELECT make_time(
extract(hour from d)::integer,
(trunc(extract(minute from d) / round_to))::integer * round_to,
0.0
)
FROM unrounded;
$$ LANGUAGE sql;
/*
Uniform distribution of integer random numbers.
*/
CREATE OR REPLACE FUNCTION rnd_uniform(a integer, b integer)
RETURNS integer
AS $$
SELECT floor(random() * (b-a+1) + a)::integer
$$ LANGUAGE sql;
/*
Binomial distribution.
*/
CREATE OR REPLACE FUNCTION rnd_binomial(
n integer,
p float
)
RETURNS integer
AS $$
DECLARE
res integer;
n0 integer;
BEGIN
res := 0;
WHILE (n > 0) LOOP
n0 := least(n, 30);
res := res + rnd_binomial0(n0,p); -- preventing underflow
n := n - n0;
END LOOP;
RETURN res;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION rnd_binomial0(
n integer,
p float
)
RETURNS integer
AS $$
DECLARE
r float := (1 - p)^n;
s float := r;
k integer := 0;
c float := p / (1 - p);
a float := random();
BEGIN
WHILE a > s LOOP
k := k + 1;
r := r * c * (n - k + 1) / k;
s := s + r;
END LOOP;
RETURN k;
END;
$$ LANGUAGE plpgsql;
/*
Exponential distribution (lambda = 1).
For Poisson process:
E := rnd_exponential
T = T + E / rate
*/
CREATE OR REPLACE FUNCTION rnd_exponential(
lambda float
)
RETURNS float
AS $$
SELECT -(1.0/lambda)*ln(1 - random());
$$ LANGUAGE sql;
/*
Erlang distribution.
*/
CREATE OR REPLACE FUNCTION rnd_erlang(
n integer,
p float
)
RETURNS float
LANGUAGE sql
BEGIN ATOMIC
SELECT sum(rnd_exponential(1/p)) FROM generate_series(1,n);
END;
/*
Standard normal distribution (mean = 0, std deviation = 1).
Can replace with stock random_normal() in PG16.
*/
CREATE OR REPLACE FUNCTION rnd_normal()
RETURNS float
AS $$
DECLARE
b1 float;
b2 float;
d float;
BEGIN
LOOP
b1 := 2.0 * random() - 1.0;
b2 := 2.0 * random() - 1.0;
d := b1^2 + b2^2;
EXIT WHEN d <= 1.0;
END LOOP;
RETURN b1 * sqrt( -2.0 * ln(d) / d );
-- b2 can be used as another independent random variable,
-- although we do not use this opportunity
END;
$$ LANGUAGE plpgsql;