1*22cd51feSMatthew Dillon /* $OpenBSD: arc4random_uniform.c,v 1.3 2019/01/20 02:59:07 bcook Exp $ */
2*22cd51feSMatthew Dillon
3*22cd51feSMatthew Dillon /*
4*22cd51feSMatthew Dillon * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
5*22cd51feSMatthew Dillon *
6*22cd51feSMatthew Dillon * Permission to use, copy, modify, and distribute this software for any
7*22cd51feSMatthew Dillon * purpose with or without fee is hereby granted, provided that the above
8*22cd51feSMatthew Dillon * copyright notice and this permission notice appear in all copies.
9*22cd51feSMatthew Dillon *
10*22cd51feSMatthew Dillon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*22cd51feSMatthew Dillon * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*22cd51feSMatthew Dillon * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*22cd51feSMatthew Dillon * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*22cd51feSMatthew Dillon * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*22cd51feSMatthew Dillon * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*22cd51feSMatthew Dillon * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*22cd51feSMatthew Dillon *
18*22cd51feSMatthew Dillon * $FreeBSD$
19*22cd51feSMatthew Dillon */
20*22cd51feSMatthew Dillon
21*22cd51feSMatthew Dillon #include <stdint.h>
22*22cd51feSMatthew Dillon #include <stdlib.h>
23*22cd51feSMatthew Dillon
24*22cd51feSMatthew Dillon /*
25*22cd51feSMatthew Dillon * Calculate a uniformly distributed random number less than upper_bound
26*22cd51feSMatthew Dillon * avoiding "modulo bias".
27*22cd51feSMatthew Dillon *
28*22cd51feSMatthew Dillon * Uniformity is achieved by generating new random numbers until the one
29*22cd51feSMatthew Dillon * returned is outside the range [0, 2**32 % upper_bound). This
30*22cd51feSMatthew Dillon * guarantees the selected random number will be inside
31*22cd51feSMatthew Dillon * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
32*22cd51feSMatthew Dillon * after reduction modulo upper_bound.
33*22cd51feSMatthew Dillon */
34*22cd51feSMatthew Dillon uint32_t
arc4random_uniform(uint32_t upper_bound)35*22cd51feSMatthew Dillon arc4random_uniform(uint32_t upper_bound)
36*22cd51feSMatthew Dillon {
37*22cd51feSMatthew Dillon uint32_t r, min;
38*22cd51feSMatthew Dillon
39*22cd51feSMatthew Dillon if (upper_bound < 2)
40*22cd51feSMatthew Dillon return 0;
41*22cd51feSMatthew Dillon
42*22cd51feSMatthew Dillon /* 2**32 % x == (2**32 - x) % x */
43*22cd51feSMatthew Dillon min = -upper_bound % upper_bound;
44*22cd51feSMatthew Dillon
45*22cd51feSMatthew Dillon /*
46*22cd51feSMatthew Dillon * This could theoretically loop forever but each retry has
47*22cd51feSMatthew Dillon * p > 0.5 (worst case, usually far better) of selecting a
48*22cd51feSMatthew Dillon * number inside the range we need, so it should rarely need
49*22cd51feSMatthew Dillon * to re-roll.
50*22cd51feSMatthew Dillon */
51*22cd51feSMatthew Dillon for (;;) {
52*22cd51feSMatthew Dillon r = arc4random();
53*22cd51feSMatthew Dillon if (r >= min)
54*22cd51feSMatthew Dillon break;
55*22cd51feSMatthew Dillon }
56*22cd51feSMatthew Dillon
57*22cd51feSMatthew Dillon return r % upper_bound;
58*22cd51feSMatthew Dillon }
59