Drizzled Public API Documentation

ut0rnd.cc

00001 /*****************************************************************************
00002 
00003 Copyright (C) 1994, 2009, Innobase Oy. All Rights Reserved.
00004 
00005 This program is free software; you can redistribute it and/or modify it under
00006 the terms of the GNU General Public License as published by the Free Software
00007 Foundation; version 2 of the License.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT
00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00012 
00013 You should have received a copy of the GNU General Public License along with
00014 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
00015 St, Fifth Floor, Boston, MA 02110-1301 USA
00016 
00017 *****************************************************************************/
00018 
00019 /***************************************************************/
00026 #include "ut0rnd.h"
00027 
00028 #ifdef UNIV_NONINL
00029 #include "ut0rnd.ic"
00030 #endif
00031 
00034 #define UT_RANDOM_1 1.0412321
00035 #define UT_RANDOM_2 1.1131347
00036 #define UT_RANDOM_3 1.0132677
00037 
00040 UNIV_INTERN ulint ut_rnd_ulint_counter = 65654363;
00041 
00042 /***********************************************************/
00046 UNIV_INTERN
00047 ulint
00048 ut_find_prime(
00049 /*==========*/
00050   ulint n)  
00051 {
00052   ulint pow2;
00053   ulint i;
00054 
00055   n += 100;
00056 
00057   pow2 = 1;
00058   while (pow2 * 2 < n) {
00059     pow2 = 2 * pow2;
00060   }
00061 
00062   if ((double)n < 1.05 * (double)pow2) {
00063     n = (ulint) ((double)n * UT_RANDOM_1);
00064   }
00065 
00066   pow2 = 2 * pow2;
00067 
00068   if ((double)n > 0.95 * (double)pow2) {
00069     n = (ulint) ((double)n * UT_RANDOM_2);
00070   }
00071 
00072   if (n > pow2 - 20) {
00073     n += 30;
00074   }
00075 
00076   /* Now we have n far enough from powers of 2. To make
00077   n more random (especially, if it was not near
00078   a power of 2), we then multiply it by a random number. */
00079 
00080   n = (ulint) ((double)n * UT_RANDOM_3);
00081 
00082   for (;; n++) {
00083     i = 2;
00084     while (i * i <= n) {
00085       if (n % i == 0) {
00086         goto next_n;
00087       }
00088       i++;
00089     }
00090 
00091     /* Found a prime */
00092     break;
00093 next_n:   ;
00094   }
00095 
00096   return(n);
00097 }