This commit is contained in:
2025-07-07 20:27:29 +02:00
parent 9f073b91b5
commit b7ca7f78c1
2 changed files with 48 additions and 0 deletions

View File

@@ -61,11 +61,25 @@ nn_Alloc nn_libcAllocator() {
};
}
static size_t nni_rand(void *userdata) {
return rand();
}
nn_Rng nn_libcRng() {
srand(time(NULL));
return (nn_Rng) {
.userdata = NULL,
.maximum = RAND_MAX,
.proc = nni_rand,
};
}
nn_Context nn_libcContext() {
return (nn_Context) {
.allocator = nn_libcAllocator(),
.clock = nn_libcRealTime(),
.lockManager = nn_libcMutex(),
.rng = nn_libcRng(),
};
}
#endif
@@ -89,6 +103,23 @@ void nn_deallocStr(nn_Alloc *alloc, char *s) {
nn_dealloc(alloc, s, strlen(s)+1);
}
size_t nn_rand(nn_Rng *rng) {
return rng->proc(rng->userdata);
}
// returns from 0 to 1 (inclusive)
double nn_randf(nn_Rng *rng) {
double x = nn_rand(rng);
return x / rng->maximum;
}
// returns from 0 to 1 (exclusive)
double nn_randfe(nn_Rng *rng) {
double x = nn_rand(rng);
if(x >= rng->maximum) return 0;
return x / rng->maximum;
}
#ifndef NN_BAREMETAL
#ifdef NN_POSIX