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