randpup.git
util.c

/* randpup - ngram text generator kernel module
* Copyright (C) 2025 ArcNyxx
* see LICENCE.MIT file for licensing information */
#include "util.h"
#ifdef __KERNEL__
#include <linux/limits.h>
#include <linux/random.h>
#define RANDOM(var) get_random_bytes(&var, sizeof(var))
#else /* __KERNEL__ */
#include <limits.h>
#include <sys/random.h>
#define RANDOM(var) getrandom(&var, sizeof(var), 0)
#endif /* __KERNEL__ */
int
pup_rand(int lower, int upper)
{
unsigned int rand;
const unsigned int max = UINT_MAX - UINT_MAX % (upper - lower);
do
RANDOM(rand);
while (rand > max);
return rand % (upper - lower) + lower;
}