Visualization Library 2.1.0

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Random.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlCore/Random.hpp>
33 #include <vlCore/Time.hpp>
34 #include <vlCore/Log.hpp>
36 #include <cstdlib>
37 
38 #if defined(VL_PLATFORM_WINDOWS)
39  #include <wincrypt.h>
40 #endif
41 
42 using namespace vl;
43 
44 //-----------------------------------------------------------------------------
46 {
47  VL_DEBUG_SET_OBJECT_NAME()
48 #if defined(_MSC_VER) || defined(__MINGW32__)
49  hCryptProv = NULL;
50  if( !CryptAcquireContext( (HCRYPTPROV*)&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0) )
51  hCryptProv = NULL;
52 #elif defined(__GNUG__) && !defined(__MINGW32__)
53  mDefURandom = fopen("/dev/urandom", "rb");
54 #endif
55 }
56 //-----------------------------------------------------------------------------
58 {
59 #if defined(_MSC_VER) || defined(__MINGW32__)
60  if( hCryptProv )
61  {
62  CryptReleaseContext( (HCRYPTPROV)hCryptProv, 0 );
63  hCryptProv = NULL;
64  }
65 #elif defined(__GNUG__) && !defined(__MINGW32__)
66  if (mDefURandom)
67  {
68  fclose(mDefURandom);
69  mDefURandom = NULL;
70  }
71 #endif
72 }
73 //-----------------------------------------------------------------------------
74 bool Random::fillRandom(void* ptr, size_t bytes) const
75 {
76 #if defined(_MSC_VER) || defined(__MINGW32__)
77  if( !(hCryptProv && CryptGenRandom( (HCRYPTPROV)hCryptProv, bytes, (BYTE*)ptr)) )
78  {
79  fillRandomMersenneTwister(ptr, bytes);
80  return false;
81  }
82  else
83  return true;
84 #elif defined(__GNUG__) && !defined(__MINGW32__)
85  if ( mDefURandom && fread(ptr, 1, bytes, mDefURandom) == bytes )
86  return true;
87  else
88  {
89  fillRandomMersenneTwister(ptr, bytes);
90  return false;
91  }
92 #else
93  fillRandomMersenneTwister(ptr, bytes);
94  return false;
95 #endif
96 }
97 //-----------------------------------------------------------------------------
98 void Random::fillRandomMersenneTwister(void* ptr, size_t bytes) const
99 {
100  unsigned int rnd = 0;
101 
102  unsigned char* cptr = (unsigned char*)ptr;
103  memset(cptr, 0, bytes);
104  for (size_t i=0; i<bytes; ++i)
105  {
106  defMersenneTwister()->randInt( rnd );
107  cptr[i] ^= (rnd>>0) & 0xFF;
108  cptr[i] ^= (rnd>>8) & 0xFF;
109  cptr[i] ^= (rnd>>16) & 0xFF;
110  cptr[i] ^= (rnd>>12) & 0xFF;
111  }
112 }
113 //-----------------------------------------------------------------------------
void fillRandomMersenneTwister(void *ptr, size_t bytes) const
Fills the specified buffer with random data generated using a defMersienneTwister().
Definition: Random.cpp:98
virtual ~Random()
Destructor.
Definition: Random.cpp:57
Visualization Library main namespace.
Random()
Constructor.
Definition: Random.cpp:45
#define NULL
Definition: OpenGLDefs.hpp:81
virtual bool fillRandom(void *ptr, size_t bytes) const
Fills the specified buffer with random data generated using the best quality random number generation...
Definition: Random.cpp:74
VLCORE_EXPORT MersenneTwister * defMersenneTwister()
Definition: pimpl.cpp:112