]> mj.ucw.cz Git - libucw.git/blob - lib/random.c
Signedness fixes.
[libucw.git] / lib / random.c
1 /*
2  *      Sherlock Library -- Unbiased Range Correction for random()
3  *
4  *      (c) 1998 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include "lib.h"
11
12 uns
13 random_max(uns max)
14 {
15   uns r, l;
16
17   l = (RAND_MAX + 1U) - ((RAND_MAX + 1U) % max);
18   do
19     r = random();
20   while (r >= l);
21   return r % max;
22 }