-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrand.c
More file actions
44 lines (40 loc) · 1.04 KB
/
rand.c
File metadata and controls
44 lines (40 loc) · 1.04 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
#include <stdlib.h>
#include <sys/time.h>
#include <errno.h>
#include "object.h"
#include "vm.h"
#include "runtime.h"
#include "memory.h"
#include "table.h"
unsigned randSeed = 1;
static Value lxRandom(int argCount, Value *args) {
CHECK_ARITY("random", 0, 1, argCount);
long res = random();
if (argCount == 1) {
Value maxVal = args[0];
CHECK_ARG_BUILTIN_TYPE(maxVal, IS_NUMBER_FUNC, "number", 1);
double max = AS_NUMBER(maxVal);
if (max < 0) max = -max;
if (max == 0) {
return NUMBER_VAL(0);
} else {
return NUMBER_VAL(res % (int)max);
}
} else {
return NUMBER_VAL(res);
}
}
void Init_rand() {
struct timeval tv;
int res = gettimeofday(&tv, NULL);
unsigned seed = randSeed;
if (res == -1) {
fprintf(stderr, "gettimeofday() failed in Init_rand(): %s\n", strerror(errno));
errno = 0;
} else {
seed = (unsigned)tv.tv_sec;
}
randSeed = seed;
srandom(randSeed);
addGlobalFunction("random", lxRandom);
}