RandomNumber.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00038 #include "blocxx/BLOCXX_config.h"
00039 #include "blocxx/RandomNumber.hpp"
00040 #include "blocxx/Assertion.hpp"
00041 #include "blocxx/ThreadOnce.hpp"
00042 #include "blocxx/Mutex.hpp"
00043 #include "blocxx/MutexLock.hpp"
00044 #include <fstream>
00045 #include <sys/types.h>
00046 
00047 #ifdef BLOCXX_HAVE_UNISTD_H
00048 #include <unistd.h>
00049 #endif
00050 
00051 #ifdef BLOCXX_HAVE_SYS_TIME_H
00052 #include <sys/time.h>
00053 #endif
00054 
00055 #include <stdlib.h>
00056 #include <time.h>
00057 
00058 namespace BLOCXX_NAMESPACE
00059 {
00060 
00062 namespace
00063 {
00064 OnceFlag guard; 
00065 unsigned int seed = 0; 
00066 }
00067 
00069 RandomNumber::RandomNumber(Int32 lowVal, Int32 highVal)
00070 : m_lowVal(lowVal), m_highVal(highVal)
00071 {
00072    if (lowVal > highVal)
00073    {
00074       m_lowVal = highVal;
00075       m_highVal = lowVal;
00076    }
00077    callOnce(guard, &initRandomness); 
00078 }
00079 
00081 void
00082 RandomNumber::initRandomness()
00083 {
00084 #ifdef BLOCXX_WIN32
00085          time_t timeval = ::time(NULL);
00086          seed = timeval;
00087 #else
00088          // use the time as part of the seed
00089          struct timeval tv;
00090          gettimeofday(&tv, 0);
00091          // try to get something from the kernel
00092          std::ifstream infile("/dev/urandom", std::ios::in);
00093          if (!infile)
00094          {
00095             infile.open("/dev/random", std::ios::in);
00096          }
00097          // don't initialize this, we may get random stack
00098          // junk in case infile isn't usable.
00099          unsigned int dev_rand_input;
00100          if (infile)
00101          {
00102             infile.read(reinterpret_cast<char*>(&dev_rand_input), sizeof(dev_rand_input));
00103             infile.close();
00104          }
00105          // Build the seed. Take into account our pid and uid.
00106          seed = dev_rand_input ^ (getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec;
00107 #endif
00108 #ifdef BLOCXX_HAVE_SRANDOM
00109          srandom(seed);
00110 #else
00111          srand(seed);
00112 #endif
00113 }
00114 
00116 void
00117 RandomNumber::saveRandomState()
00118 {
00119    // Do nothing.  This function is so that RandomNumber has the same
00120    // interface as CryptographicRandomNumber
00121 }
00122 
00123 namespace
00124 {
00125 Mutex g_guard; 
00126 }
00127    
00129 Int32
00130 RandomNumber::getNextNumber()
00131 {
00132    MutexLock lock(g_guard);
00133 #ifdef BLOCXX_HAVE_RANDOM
00134    return m_lowVal + (random() % (m_highVal - m_lowVal + 1));
00135 #else
00136    return m_lowVal + (rand() % (m_highVal - m_lowVal + 1));
00137 #endif
00138 }
00139 
00140 } // end namespace BLOCXX_NAMESPACE
00141 

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6