2022-08-26 17:25:45 +08:00
|
|
|
#include "random.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2023-07-09 23:12:21 +08:00
|
|
|
void random___init__(PikaObj* self) {
|
2023-02-01 00:41:19 +08:00
|
|
|
srand(pika_platform_get_tick());
|
2022-08-26 17:25:45 +08:00
|
|
|
}
|
|
|
|
|
2023-07-09 23:12:21 +08:00
|
|
|
int random_randint(PikaObj* self, int a, int b) {
|
2022-08-26 17:25:45 +08:00
|
|
|
return rand() % (b - a + 1) + a;
|
|
|
|
}
|
|
|
|
|
2023-07-09 23:12:21 +08:00
|
|
|
pika_float random_random(PikaObj* self) {
|
2022-09-28 11:23:39 +08:00
|
|
|
return rand() / (pika_float)RAND_MAX;
|
2022-08-26 17:25:45 +08:00
|
|
|
}
|
|
|
|
|
2023-07-09 23:12:21 +08:00
|
|
|
int random_randrange(PikaObj* self, int start, int stop, int step) {
|
2022-08-26 17:25:45 +08:00
|
|
|
if (step == 0) {
|
|
|
|
return random_randint(self, start, stop);
|
|
|
|
}
|
|
|
|
int n = (stop - start + step) / step;
|
|
|
|
int r = random_randint(self, 0, n - 1);
|
|
|
|
return start + r * step;
|
|
|
|
}
|
|
|
|
|
2023-07-09 23:12:21 +08:00
|
|
|
PikaObj* random_seed(PikaObj* self, int a) {
|
2022-08-26 17:25:45 +08:00
|
|
|
srand(a);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-07-09 23:12:21 +08:00
|
|
|
pika_float random_uniform(PikaObj* self, pika_float a, pika_float b) {
|
2022-08-26 17:25:45 +08:00
|
|
|
return (b - a) * random_random(self) + a;
|
|
|
|
}
|