You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.3 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1994, 2009, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  12. *****************************************************************************/
  13. /***************************************************************//**
  14. @file ut/ut0rnd.cc
  15. Random numbers and hashing
  16. Created 5/11/1994 Heikki Tuuri
  17. ********************************************************************/
  18. #include "ut0rnd.h"
  19. #ifdef UNIV_NONINL
  20. #include "ut0rnd.ic"
  21. #endif
  22. /** These random numbers are used in ut_find_prime */
  23. /*@{*/
  24. #define UT_RANDOM_1 1.0412321
  25. #define UT_RANDOM_2 1.1131347
  26. #define UT_RANDOM_3 1.0132677
  27. /*@}*/
  28. /** Seed value of ut_rnd_gen_ulint(). */
  29. UNIV_INTERN ulint ut_rnd_ulint_counter = 65654363;
  30. /***********************************************************//**
  31. Looks for a prime number slightly greater than the given argument.
  32. The prime is chosen so that it is not near any power of 2.
  33. @return prime */
  34. UNIV_INTERN
  35. ulint
  36. ut_find_prime(
  37. /*==========*/
  38. ulint n) /*!< in: positive number > 100 */
  39. {
  40. ulint pow2;
  41. ulint i;
  42. n += 100;
  43. pow2 = 1;
  44. while (pow2 * 2 < n) {
  45. pow2 = 2 * pow2;
  46. }
  47. if ((double) n < 1.05 * (double) pow2) {
  48. n = (ulint) ((double) n * UT_RANDOM_1);
  49. }
  50. pow2 = 2 * pow2;
  51. if ((double) n > 0.95 * (double) pow2) {
  52. n = (ulint) ((double) n * UT_RANDOM_2);
  53. }
  54. if (n > pow2 - 20) {
  55. n += 30;
  56. }
  57. /* Now we have n far enough from powers of 2. To make
  58. n more random (especially, if it was not near
  59. a power of 2), we then multiply it by a random number. */
  60. n = (ulint) ((double) n * UT_RANDOM_3);
  61. for (;; n++) {
  62. i = 2;
  63. while (i * i <= n) {
  64. if (n % i == 0) {
  65. goto next_n;
  66. }
  67. i++;
  68. }
  69. /* Found a prime */
  70. break;
  71. next_n: ;
  72. }
  73. return(n);
  74. }