arc4random_uniform.c 1.8 KB

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