Home | History | Annotate | Download | only in db_vlvm
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /* $Id: db_utilities_random.h,v 1.1 2010/08/19 18:09:20 bsouthall Exp $ */
     18 
     19 #ifndef DB_UTILITIES_RANDOM
     20 #define DB_UTILITIES_RANDOM
     21 
     22 #include "db_utilities.h"
     23 
     24 
     25 
     26 /*****************************************************************
     27 *    Lean and mean begins here                                   *
     28 *****************************************************************/
     29 /*!
     30  * \defgroup LMRandom (LM) Random numbers, random sampling
     31  */
     32 /*\{*/
     33 /*!
     34  Random Number generator. Initialize with non-zero
     35 integer value r. A double between zero and one is
     36 returned.
     37 \param r    seed
     38 \return random double
     39 */
     40 inline double db_QuickRandomDouble(int &r)
     41 {
     42     int c;
     43     c=r/127773;
     44     r=16807*(r-c*127773)-2836*c;
     45     if(r<0) r+=2147483647;
     46     return((1.0/((double)2147483647))*r);
     47     //return (((double)rand())/(double)RAND_MAX);
     48 }
     49 
     50 /*!
     51 Random Number generator. Initialize with non-zero
     52 integer value r. An int between and including 0 and max
     53  \param r    seed
     54  \param max    upped limit
     55  \return random int
     56 */
     57 inline int db_RandomInt(int &r,int max)
     58 {
     59     double dtemp;
     60     int itemp;
     61     dtemp=db_QuickRandomDouble(r)*(max+1);
     62     itemp=(int) dtemp;
     63     if(itemp<=0) return(0);
     64     if(itemp>=max) return(max);
     65     return(itemp);
     66 }
     67 
     68 /*!
     69  Generate a random sample indexing into [0..pool_size-1].
     70  \param s            sample (out) pre-allocated array of size sample_size
     71  \param sample_size    size of sample
     72  \param pool_size    upper limit on item index
     73  \param r_seed        random number generator seed
     74  */
     75 inline void db_RandomSample(int *s,int sample_size,int pool_size,int &r_seed)
     76 {
     77     int temp,temp2,i,j;
     78 
     79     for(i=0;i<sample_size;i++)
     80     {
     81         temp=db_RandomInt(r_seed,pool_size-1-i);
     82 
     83         for(j=0;j<i;j++)
     84         {
     85             if(s[j]<=temp) temp++;
     86             else
     87             {
     88                 /*swap*/
     89                 temp2=temp;
     90                 temp=s[j];
     91                 s[j]=temp2;
     92             }
     93         }
     94         s[i]=temp;
     95     }
     96 }
     97 /*\}*/
     98 #endif /* DB_UTILITIES_RANDOM */
     99