util: Redefine method for random number generation

The previous definitions random::generateWord* now generate
(cryptographically) "non-secure" random numbers from uniform
distribution using Boost.Random routines.  When (cryptographically)
secure random numbers are necessary, random::generateSecureWord*
routines should be used instead.

Change-Id: I52561119f7e558b4cfe0d250ca8a89e550d21b5f
Refs: #1760
diff --git a/src/util/random.cpp b/src/util/random.cpp
index 3310d1b..f321bbd 100644
--- a/src/util/random.cpp
+++ b/src/util/random.cpp
@@ -23,30 +23,66 @@
 
 #include "random.hpp"
 
+#include <boost/nondet_random.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
+
 #include "../security/cryptopp.hpp"
 
 namespace ndn {
 namespace random {
 
-uint32_t
-generateWord32()
+// CryptoPP-based (secure) random generators
+
+static CryptoPP::AutoSeededRandomPool&
+getSecureRandomGenerator()
 {
   static CryptoPP::AutoSeededRandomPool rng;
 
-  return rng.GenerateWord32();
+  return rng;
+}
+
+uint32_t
+generateSecureWord32()
+{
+  return getSecureRandomGenerator().GenerateWord32();
+}
+
+uint64_t
+generateSecureWord64()
+{
+  uint64_t random;
+  getSecureRandomGenerator()
+    .GenerateBlock(reinterpret_cast<unsigned char*>(&random), sizeof(uint64_t));
+
+  return random;
+}
+
+// Boost.Random-based (simple) random generators
+
+static boost::random::mt19937&
+getRandomGenerator()
+{
+  static boost::random_device randomSeedGenerator;
+  static boost::random::mt19937 gen(randomSeedGenerator);
+
+  return gen;
+}
+
+uint32_t
+generateWord32()
+{
+  static boost::random::uniform_int_distribution<uint32_t> distribution;
+  return distribution(getRandomGenerator());
 }
 
 uint64_t
 generateWord64()
 {
-  static CryptoPP::AutoSeededRandomPool rng;
-
-  uint64_t random;
-
-  rng.GenerateBlock(reinterpret_cast<unsigned char*>(&random), 8);
-
-  return random;
+  static boost::random::uniform_int_distribution<uint64_t> distribution;
+  return distribution(getRandomGenerator());
 }
 
+
 } // namespace random
 } // namespace ndn
diff --git a/src/util/random.hpp b/src/util/random.hpp
index afa1758..6dc1b52 100644
--- a/src/util/random.hpp
+++ b/src/util/random.hpp
@@ -27,9 +27,43 @@
 namespace ndn {
 namespace random {
 
+/**
+ * @brief Generate a cryptographically secure random integer from the range [0, 2^32)
+ *
+ * This method uses CryptoPP routines
+ */
+uint32_t
+generateSecureWord32();
+
+/**
+ * @brief Generate a cryptographically secure random integer from the range [0, 2^64)
+ *
+ * This method uses CryptoPP routines
+ */
+uint64_t
+generateSecureWord64();
+
+/**
+ * @brief Generate a cryptographically non-secure random integer from the range [0, 2^32)
+ *
+ * This method uses Boost.Random routines
+ *
+ * This version is faster than generateSecureWord32, but it should not be used when
+ * cryptographically secure random integers are needed (e.g., when creating signing or
+ * encryption keys)
+ */
 uint32_t
 generateWord32();
 
+/**
+ * @brief Generate a cryptographically non-secure random integer from range [0, 2^64)
+ *
+ * This method uses Boost.Random routines
+ *
+ * This version is faster than generateSecureWord64, but it should not be used when
+ * cryptographically secure random integers are needed (e.g., when creating signing or
+ * encryption keys)
+ */
 uint64_t
 generateWord64();
 
diff --git a/tests/unit-tests/util/test-random.cpp b/tests/unit-tests/util/test-random.cpp
new file mode 100644
index 0000000..26ac8ce
--- /dev/null
+++ b/tests/unit-tests/util/test-random.cpp
@@ -0,0 +1,111 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "util/random.hpp"
+
+#include "boost-test.hpp"
+#include <boost/mpl/vector.hpp>
+
+#include <cmath>
+
+namespace ndn {
+
+BOOST_AUTO_TEST_SUITE(UtilTestRandom)
+
+class PseudoRandomWord32
+{
+public:
+  static uint32_t
+  generate()
+  {
+    return random::generateWord32();
+  }
+};
+
+class PseudoRandomWord64
+{
+public:
+  static uint64_t
+  generate()
+  {
+    return random::generateWord64();
+  }
+};
+
+class SecureRandomWord32
+{
+public:
+  static uint32_t
+  generate()
+  {
+    return random::generateSecureWord32();
+  }
+};
+
+class SecureRandomWord64
+{
+public:
+  static uint64_t
+  generate()
+  {
+    return random::generateSecureWord64();
+  }
+};
+
+typedef boost::mpl::vector<PseudoRandomWord32,
+                           PseudoRandomWord64,
+                           SecureRandomWord32,
+                           SecureRandomWord64> RandomGenerators;
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(GoodnessOfFit, RandomGenerator, RandomGenerators)
+{
+  // Kolmogorov-Smirnov Goodness-of-Fit Test
+  // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm
+
+  const size_t MAX_BINS = 32;
+  const uint32_t MAX_ITERATIONS = 35;
+
+  std::vector<uint32_t> counts(MAX_BINS, 0);
+
+  for (uint32_t i = 0; i < MAX_ITERATIONS; i++) {
+    counts[RandomGenerator::generate() % MAX_BINS]++;
+  }
+
+  std::vector<double> edf(MAX_BINS, 0.0);
+  double probability = 0.0;
+  for (size_t i = 0; i < MAX_BINS; i++) {
+    probability += 1.0 * counts[i] / MAX_ITERATIONS;
+    edf[i] = probability;
+  }
+
+  double t = 0.0;
+  for (size_t i = 0; i < MAX_BINS; i++) {
+    t = std::max(t, std::abs(edf[i] - (i * 1.0 / MAX_BINS)));
+  }
+
+  // Check if it is uniform distribution with confidence 0.95
+  // http://dlc.erieri.com/onlinetextbook/index.cfm?fuseaction=textbook.appendix&FileName=Table7
+  BOOST_WARN_LE(t, 0.230);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn
diff --git a/wscript b/wscript
index 0f168b7..567d97d 100644
--- a/wscript
+++ b/wscript
@@ -68,7 +68,7 @@
         conf.define('HAVE_CXX11', 1)
 
     USED_BOOST_LIBS = ['system', 'filesystem', 'date_time', 'iostreams',
-                       'regex', 'program_options', 'chrono']
+                       'regex', 'program_options', 'chrono', 'random']
     if conf.env['WITH_TESTS']:
         USED_BOOST_LIBS += ['unit_test_framework']
         conf.define('HAVE_TESTS', 1)