The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/arc4random_uniform.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 6c768844f15f6e9ea4a5a75efe726452


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.