blob: dea81aa42e8c497107dbd43b1ddb19a4ee56b527 (
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
|
#include <stdlib.h>
#include "api/vm.h"
#include "api/util.h"
#include "api/int.h"
#include "api/nil.h"
#include "common/err.h"
UwUVMValue uwu_random(UwUVMArgs *args)
{
uwuutil_require_exact("nolambda:random:random", args, 2);
UwUVMValue value0 = uwuvm_get_arg(args, 0);
if (value0.type != &uwuint_type)
error("type error: nolamda:random:random requires an integer as $0\n");
UwUVMValue value1 = uwuvm_get_arg(args, 1);
if (value1.type != &uwuint_type)
error("type error: nolamda:random:random requires an integer as $1\n");
long min = uwuint_get(value0);
long max = uwuint_get(value1) + 1;
long range = max - min;
if (range < 0)
error("type error: range passed to nolambda:random:random is empty\n");
if (range > RAND_MAX)
error("type error: range passed to nolambda:random:random is bigger than nolambda:random:max");
return uwuint_create(min + rand() % range);
}
UwUVMValue uwu_max(UwUVMArgs *args)
{
uwuutil_require_exact("nolambda:random:max", args, 0);
return uwuint_create(RAND_MAX);
}
UwUVMValue uwu_seed(UwUVMArgs *args)
{
uwuutil_require_exact("nolambda:random:seed", args, 1);
UwUVMValue value = uwuvm_get_arg(args, 0);
if (value.type != &uwuint_type)
error("type error: nolamda:random:seed requires an integer as $0\n");
srand(uwuint_get(value) % RAND_MAX);
return uwunil_create();
}
|