Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

int Impala::Util::Random::GetInt ( int  upperBound  )  [inline]

Gives a random integer in range [0,i).

Definition at line 76 of file Random.h.

References GetRandMax(), ILOG_ERROR, ILOG_WARNING, and Next().

00077     {
00078         if(upperBound < 0)
00079         {
00080             ILOG_ERROR("upperbound must be positive");
00081             return 0;
00082         }
00083         if(upperBound > GetRandMax())
00084             ILOG_WARNING(upperBound <<" upperBound > rand max "<< GetRandMax());
00085         
00086         // special case for when upperBound is a power of two: return the
00087         // higher-order bits in stead of the lower order. These have better
00088         // statistical properties.
00089         if(!((upperBound -1) & upperBound)) // (x-1)&x is 0 iff x is a power of two
00090             return (int)((upperBound * (UInt64)Next(31)) >> 31);
00091 
00092         // Because of the modulo some values would be returned more than
00093         // others. This loop rejects these numbers
00094         int bits, val;
00095         do {
00096             bits = Next(31);
00097             val = bits % upperBound;
00098         } while(bits - val + (upperBound-1) < 0); // this check works because of
00099                                                   // overflow
00100         return val;
00101     }

Here is the call graph for this function:


Generated on Thu Jan 13 09:24:20 2011 for ImpalaSrc by  doxygen 1.5.1