-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandom.cc
370 lines (325 loc) · 12.2 KB
/
Random.cc
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "redismodule.h"
#include <random>
#include <cmath>
extern "C" {
// int RandomUnif_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
// int RandomExp_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
}
// Random device and Mersenne Twister engine are shared across commands
std::random_device rd;
std::mt19937 gen(rd());
/* RANDOM.DUNIF START END */
int RandomDUnif_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 3) return RedisModule_WrongArity(ctx);
long long start, end;
if (
(RedisModule_StringToLongLong(argv[1],&start) != REDISMODULE_OK) ||
(RedisModule_StringToLongLong(argv[2],&end) != REDISMODULE_OK) ||
(start > end)
)
return RedisModule_ReplyWithError(ctx,"ERR invalid range");
std::uniform_int_distribution<long long> rdunif(start,end);
RedisModule_ReplyWithLongLong(ctx,rdunif(gen));
return REDISMODULE_OK;
}
/* RANDOM.UNIF START END */
int RandomUnif_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 3) return RedisModule_WrongArity(ctx);
double start, end;
if (
(RedisModule_StringToDouble(argv[1],&start) != REDISMODULE_OK) ||
(RedisModule_StringToDouble(argv[2],&end) != REDISMODULE_OK) ||
(start > end)
)
return RedisModule_ReplyWithError(ctx,"ERR invalid range");
std::uniform_real_distribution<double> runif(start,end);
RedisModule_ReplyWithDouble(ctx,runif(gen));
return REDISMODULE_OK;
}
/* RANDOM.NORM [MEAN=0.0] [STDDEV=1.0] */
int RandomNorm_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
double mean=0.0, sd=1.0;
if (argc > 3) return RedisModule_WrongArity(ctx);
if (argc >= 2) /* Get mean */
{
if (RedisModule_StringToDouble(argv[1],&mean) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid mean");
}
if (argc == 3) /* get stddev */
{
if (RedisModule_StringToDouble(argv[2],&sd) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid standard deviation");
}
std::normal_distribution<double> rnorm(mean,sd);
RedisModule_ReplyWithDouble(ctx, rnorm(gen));
return REDISMODULE_OK;
}
/* RANDOM.LUNIF KEY COUNT START END */
int RandomLUnif_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
double start, end;
if (argc != 5) return RedisModule_WrongArity(ctx);
if (RedisModule_StringToDouble(argv[3],&start) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid start");
if (RedisModule_StringToDouble(argv[4],&end) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid end");
/* Open key */
RedisModuleKey *key = (RedisModuleKey *) RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
/* Key must be empty or list */
if ((RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_LIST &&
RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_EMPTY))
{
RedisModule_CloseKey(key);
return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);
}
/* Get count */
long long count;
if ((RedisModule_StringToLongLong(argv[2],&count) != REDISMODULE_OK) ||
(count < 0))
{
RedisModule_CloseKey(key);
return RedisModule_ReplyWithError(ctx,"ERR invalid count");
}
/* Push count randoms */
std::uniform_real_distribution<double> runif(start,end);
while (count-- > 0)
{
RedisModuleString *ele;
/* Make string with the same 19 digita precision of ReplyWithDouble */
ele=RedisModule_CreateStringPrintf(ctx,"%.19f",runif(gen));
RedisModule_ListPush(key,REDISMODULE_LIST_HEAD,ele);
RedisModule_FreeString(ctx,ele);
}
size_t len = RedisModule_ValueLength(key);
RedisModule_CloseKey(key);
RedisModule_ReplyWithLongLong(ctx, len);
return REDISMODULE_OK;
}
/* RANDOM.LNORM KEY COUNT [MEAN=0.0] [STDDEV=1.0] */
int RandomLNorm_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
double mean=0.0, sd=1.0;
if (argc > 5) return RedisModule_WrongArity(ctx);
if (argc >= 4) /* Get mean */
{
if (RedisModule_StringToDouble(argv[3],&mean) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid mean");
}
if (argc == 5) /* get stddev */
{
if (RedisModule_StringToDouble(argv[4],&sd) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid standard deviation");
}
/* Open key */
RedisModuleKey *key = (RedisModuleKey *) RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
/* Key must be empty or list */
if ((RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_LIST &&
RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_EMPTY))
{
RedisModule_CloseKey(key);
return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);
}
/* Get count */
long long count;
if ((RedisModule_StringToLongLong(argv[2],&count) != REDISMODULE_OK) ||
(count < 0))
{
RedisModule_CloseKey(key);
return RedisModule_ReplyWithError(ctx,"ERR invalid count");
}
/* Push count randoms */
std::normal_distribution<double> rnorm(mean,sd);
while (count-- > 0)
{
RedisModuleString *ele;
/* Make string with the same 19 digita precision of ReplyWithDouble */
ele=RedisModule_CreateStringPrintf(ctx,"%.19f",rnorm(gen));
RedisModule_ListPush(key,REDISMODULE_LIST_HEAD,ele);
RedisModule_FreeString(ctx,ele);
}
size_t len = RedisModule_ValueLength(key);
RedisModule_CloseKey(key);
RedisModule_ReplyWithLongLong(ctx, len);
return REDISMODULE_OK;
}
/* RANDOM.EXP [LAMBDA=1.0] */
int RandomExp_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
double lambda;
if (argc > 2) return RedisModule_WrongArity(ctx);
if (argc == 2)
{
if (RedisModule_StringToDouble(argv[1],&lambda) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid lambda");
}
else // If no parameter, use default lambda of 1
lambda=1.0;
std::exponential_distribution<double> rexp(lambda);
RedisModule_ReplyWithDouble(ctx, rexp(gen));
return REDISMODULE_OK;
}
/* RANDOM.LEXP KEY COUNT [LAMBDA=1.0] */
int RandomLExp_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
double lambda;
if (argc < 3 || argc > 4) return RedisModule_WrongArity(ctx);
if (argc == 4)
{
if (RedisModule_StringToDouble(argv[3],&lambda) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid lambda");
}
else // If no parameter, use default lambda of 1
lambda=1.0;
/* Open key */
RedisModuleKey *key = (RedisModuleKey *) RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
/* Key must be empty or list */
if ((RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_LIST &&
RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_EMPTY))
{
RedisModule_CloseKey(key);
return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);
}
/* Get count */
long long count;
if ((RedisModule_StringToLongLong(argv[2],&count) != REDISMODULE_OK) ||
(count < 0))
{
RedisModule_CloseKey(key);
return RedisModule_ReplyWithError(ctx,"ERR invalid count");
}
/* Push count randoms */
std::exponential_distribution<double> rexp(lambda);
while (count-- > 0)
{
RedisModuleString *ele;
/* Make string with the same 19 digita precision of ReplyWithDouble */
ele=RedisModule_CreateStringPrintf(ctx,"%.19f",rexp(gen));
RedisModule_ListPush(key,REDISMODULE_LIST_HEAD,ele);
RedisModule_FreeString(ctx,ele);
}
size_t len = RedisModule_ValueLength(key);
RedisModule_CloseKey(key);
RedisModule_ReplyWithLongLong(ctx, len);
return REDISMODULE_OK;
}
int RandomHist_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc > 4) return RedisModule_WrongArity(ctx);
long long slots=10;
long long col=0;
if (argc >= 3)
{
if (RedisModule_StringToLongLong(argv[2],&slots) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid hist size");
}
if (argc == 4)
{
if (RedisModule_StringToLongLong(argv[3],&col) != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx,"ERR invalid columns size");
}
/* Check key type */
RedisModuleKey * key = ( RedisModuleKey *)
RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ);
if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_LIST)
{
RedisModule_CloseKey(key);
return RedisModule_ReplyWithError(ctx,"ERR key not of List type");
}
RedisModule_CloseKey(key);
long long hist[slots];
for (auto i=0; i < slots; i++) hist[i]=0;
RedisModuleCallReply *reply;
reply = RedisModule_Call(ctx,"LRANGE","scc",argv[1],"0","-1");
if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_NULL ||
RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR )
{
RedisModule_FreeCallReply(reply);
return RedisModule_ReplyWithError(ctx,"ERR error in key");
}
size_t len;
double min=0, max=0;
if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ARRAY)
{
len = RedisModule_CallReplyLength(reply);
if (len == 0)
{
RedisModule_FreeCallReply(reply);
return REDISMODULE_OK;
}
for (auto i=0; i < len; i++)
{
RedisModuleCallReply *subreply;
subreply = RedisModule_CallReplyArrayElement(reply,i);
double e;
RedisModuleString *ele = RedisModule_CreateStringFromCallReply(subreply);
if (RedisModule_StringToDouble(ele,&e)==REDISMODULE_ERR)
{
RedisModule_FreeString(ctx,ele);
RedisModule_FreeCallReply(reply);
return RedisModule_ReplyWithError(ctx,"ERR bad list value");
}
RedisModule_FreeString(ctx,ele);
if (i==0) min=max=e;
if (e < min) min=e;
if (e > max) max=e;
}
double range=max-min;
for (auto i=0; i < len; i++)
{
RedisModuleCallReply *subreply;
subreply = RedisModule_CallReplyArrayElement(reply,i);
double e;
RedisModuleString *ele = RedisModule_CreateStringFromCallReply(subreply);
RedisModule_StringToDouble(ele,&e);
RedisModule_FreeString(ctx,ele);
long long slot=(long long) std::floor(((e-min)/range)*slots);
if (slot==slots) slot--; /* max value goes to last slot */
hist[slot]++;
}
}
RedisModule_FreeCallReply(reply);
RedisModule_ReplyWithArray(ctx,slots);
if (col==0)
{
for (auto i=0; i < slots; i++)
RedisModule_ReplyWithLongLong(ctx,hist[i]);
}
else
{
char s[col];
for (auto i=0; i < col; i++)
s[i] = {'*'};
double hmax=hist[0];
for (auto i=0; i < slots; i++)
if (hmax < hist[i])
hmax = hist[i];
for (auto i=0; i < slots; i++)
RedisModule_ReplyWithStringBuffer(ctx,s,std::floor(((double) hist[i])/hmax*col));
}
return REDISMODULE_OK;
}
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (RedisModule_Init(ctx,"random",1,REDISMODULE_APIVER_1)
== REDISMODULE_ERR) return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"random.dunif",
RandomDUnif_RedisCommand,"readonly random",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"random.unif",
RandomUnif_RedisCommand,"readonly random",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"random.lunif",
RandomLUnif_RedisCommand,"random",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"random.norm",
RandomNorm_RedisCommand,"readonly random",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"random.lnorm",
RandomLNorm_RedisCommand,"random",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"random.exp",
RandomExp_RedisCommand,"readonly random",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"random.lexp",
RandomLExp_RedisCommand,"random",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"random.hist",
RandomHist_RedisCommand,"readonly",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
return REDISMODULE_OK;
}