replace insecure std::rand by C++11's random number generation

fixes coverity issue 1248916 Don't call
This commit is contained in:
Dennis Luxen 2014-12-19 10:59:12 +01:00
parent d31c6fe286
commit 4b583e8ce9

View File

@ -35,7 +35,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cmath> #include <cmath>
#include <cstdio> #include <cstdio>
#include <cstdlib>
#include <fcntl.h> #include <fcntl.h>
#ifdef __linux__ #ifdef __linux__
#include <malloc.h> #include <malloc.h>
@ -45,6 +44,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include <numeric> #include <numeric>
#include <random>
#include <vector> #include <vector>
const unsigned number_of_elements = 268435456; const unsigned number_of_elements = 268435456;
@ -201,9 +201,12 @@ int main(int argc, char *argv[])
#endif #endif
// make 1000 random access, time each I/O seperately // make 1000 random access, time each I/O seperately
unsigned number_of_blocks = (number_of_elements * sizeof(unsigned) - 1) / 4096; unsigned number_of_blocks = (number_of_elements * sizeof(unsigned) - 1) / 4096;
std::random_device rd;
std::default_random_engine e1(rd());
std::uniform_int_distribution<unsigned> uniform_dist(0, number_of_blocks - 1);
for (unsigned i = 0; i < 1000; ++i) for (unsigned i = 0; i < 1000; ++i)
{ {
unsigned block_to_read = std::rand() % number_of_blocks; unsigned block_to_read =uniform_dist(e1);
off_t current_offset = block_to_read * 4096; off_t current_offset = block_to_read * 4096;
TIMER_START(random_access); TIMER_START(random_access);
#ifdef __APPLE__ #ifdef __APPLE__