# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/Random.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #include <vlCore/Random.hpp>
    00033 #include <vlCore/Time.hpp>
    00034 #include <vlCore/Log.hpp>
    00035 #include <cstdlib>
    00036
    00037 #if defined(_MSC_VER) || defined(__MINGW32__)
    00038   #include <windows.h>
    00039   #include <wincrypt.h>
    00040   // #include <NTSecAPI.h>
    00041   // #pragma comment(lib,"Advapi32.lib")
    00042 #endif
    00043
    00044 using namespace vl;
    00045
    00046 //-----------------------------------------------------------------------------
    00047 Random::Random()
    00048 {
    00049   VL_DEBUG_SET_OBJECT_NAME()
    00050 #if defined(_MSC_VER) || defined(__MINGW32__)
    00051   hCryptProv = NULL;
    00052   if( !CryptAcquireContext( (HCRYPTPROV*)&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0) )
    00053     hCryptProv = NULL;
    00054 #endif
    00055 }
    00056 //-----------------------------------------------------------------------------
    00057 Random::~Random()
    00058 {
    00059 #if defined(_MSC_VER) || defined(__MINGW32__)
    00060   if( hCryptProv  )
    00061     CryptReleaseContext( (HCRYPTPROV)hCryptProv, 0 );
    00062 #endif
    00063 }
    00064 //-----------------------------------------------------------------------------
    00065 bool Random::fillRandom(void* ptr, size_t bytes) const
    00066 {
    00067 #if defined(_MSC_VER) || defined(__MINGW32__)
    00068   if( !(hCryptProv && CryptGenRandom( (HCRYPTPROV)hCryptProv, bytes, (BYTE*)ptr)) )
    00069   {
    00070     standardFillRandom(ptr, bytes);
    00071     return false;
    00072   }
    00073   else
    00074     return true;
    00075 #elif defined(__GNUG__) && !defined(__MINGW32__)
    00076   FILE* fin = fopen("/dev/urandom", "rb");
    00077   if (fin)
    00078   {
    00079     if ( fread(ptr, 1, bytes, fin) == bytes )
    00080     {
    00081       fclose(fin);
    00082       return true;
    00083     }
    00084   }
    00085   standardFillRandom(ptr, bytes);
    00086   return false;
    00087 #else
    00088   standardFillRandom(ptr, bytes);
    00089   return false;
    00090 #endif
    00091 }
    00092 //-----------------------------------------------------------------------------
    00093 void Random::standardFillRandom(void* ptr, size_t bytes)
    00094 {
    00095   unsigned char* cptr = (unsigned char*)ptr;
    00096   memset(cptr, 0, bytes);
    00097   for (size_t i=0; i<bytes; ++i)
    00098   {
    00099     unsigned int r = (unsigned int)rand();
    00100     cptr[i] ^= (r>>0)  & 0xFF;
    00101     cptr[i] ^= (r>>8)  & 0xFF;
    00102     cptr[i] ^= (r>>16) & 0xFF;
    00103     cptr[i] ^= (r>>12) & 0xFF;
    00104   }
    00105
    00106   vl::Log::warning("Random::standardFillRandom() is being used.\n");
    00107 }
    00108 //-----------------------------------------------------------------------------
    00109 void Random::standardRandomize()
    00110 {
    00111   Time time;
    00112   int stack_pos = 0;
    00113   static int static_pos = 0;
    00114   int* dyn_pos = new int[10]; delete [] dyn_pos;
    00115   unsigned int rand_start = time.microsecond() ^ time.second() ^ time.minute() ^ time.hour() ^
    00116                             time.dayOfMonth() ^ time.month() ^ time.year() ^
    00117                             (unsigned long long)&static_pos ^ (unsigned long long)&stack_pos ^ (unsigned long long)dyn_pos;
    00118   srand(rand_start);
    00119 }
    00120 //-----------------------------------------------------------------------------

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:21.
Permission is granted to use this page to write and publish articles regarding Visualization Library.