bind9/lib/isc/include/isc/random.h
Tony Finch d20ea4a703 Make isc_random_uniform() nearly divisionless
It used to require two 32-bit integer divisions to get a random number
less than some limit. Now we use Daniel Lemire's "nearly-divisionless"
algorithm for unbiased bounded random numbers, which requires one
64-bit integer multiply in the usual case, and one 32-bit integer
division in rare slow cases. Even the slow cases are faster than
before; there are also fewer branches.

I think this algorithm is exceptionally beautiful. It also has more
clever tricks than lines of code, so I have done my best to explain
how it works.
2022-04-22 16:40:37 +01:00

71 lines
1.7 KiB
C

/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#pragma once
#include <inttypes.h>
#include <stdlib.h>
#include <isc/lang.h>
#include <isc/types.h>
/*! \file isc/random.h
* \brief Implements wrapper around a non-cryptographically secure
* pseudo-random number generator.
*
*/
ISC_LANG_BEGINDECLS
uint8_t
isc_random8(void);
/*!<
* \brief Returns a single 8-bit random value.
*/
uint16_t
isc_random16(void);
/*!<
* \brief Returns a single 16-bit random value.
*/
uint32_t
isc_random32(void);
/*!<
* \brief Returns a single 32-bit random value.
*/
void
isc_random_buf(void *buf, size_t buflen);
/*!<
* \brief Fills the region buf of length buflen with random data.
*/
uint32_t
isc_random_uniform(uint32_t upper_bound);
/*!<
* \brief Returns a single 32-bit uniformly distributed random value
* less than upper_bound.
*
* This is better than ``isc_random() % upper_bound'' as it avoids
* "modulo bias" when the upper bound is not a power of two. This
* function is also faster, because it usually avoids doing any
* divisions (which are typically very slow).
*
* It uses rejection sampling to ensure uniformity, so it may require
* multiple iterations to get a result; the probability of needing to
* resample is very small when the upper_bound is small, rising to 0.5
* when upper_bound is UINT32_MAX/2.
*/
ISC_LANG_ENDDECLS