[mlpack-git] master: Added function for obtaining a number of distinct samples. (601de29)

gitdub at mlpack.org gitdub at mlpack.org
Mon Aug 8 14:31:21 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/acd81e11579f69e75aa8406b2982328c88cf1fde...1e9f0f39ea4443f0d595c395871ea8c6b27443af

>---------------------------------------------------------------

commit 601de29c9eff6e65e9976fd146bc347c4408e8a6
Author: Mikhail Lozhnikov <lozhnikovma at gmail.com>
Date:   Fri Jul 22 18:57:47 2016 +0300

    Added function for obtaining a number of distinct samples.


>---------------------------------------------------------------

601de29c9eff6e65e9976fd146bc347c4408e8a6
 src/mlpack/core/math/random.hpp | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/src/mlpack/core/math/random.hpp b/src/mlpack/core/math/random.hpp
index a61be19..1df5b01 100644
--- a/src/mlpack/core/math/random.hpp
+++ b/src/mlpack/core/math/random.hpp
@@ -97,6 +97,44 @@ inline double RandNormal(const double mean, const double variance)
   return variance * randNormalDist(randGen) + mean;
 }
 
+/**
+ * Obtains no more than maxNumSamples distinct samples. Each sample belongs to
+ * [loInclusive, hiExclusive).
+ *
+ * @param loInclusive The lower bound (inclusive).
+ * @param hiExclusive The high bound (exclusive).
+ * @param maxNumSamples The maximum number of samples to obtain.
+ * @param distinctSamples The samples that will be obtained.
+ */
+inline void ObtainDistinctSamples(const size_t loInclusive,
+                                  const size_t hiExclusive,
+                                  const size_t maxNumSamples,
+                                  arma::uvec& distinctSamples)
+{
+  const size_t samplesRangeSize = hiExclusive - loInclusive;
+
+  if (samplesRangeSize > maxNumSamples)
+  {
+    arma::Col<size_t> samples;
+
+    samples.zeros(samplesRangeSize);
+
+    for (size_t i = 0; i < maxNumSamples; i++)
+      samples [ (size_t) math::RandInt(samplesRangeSize) ]++;
+
+    distinctSamples = arma::find(samples > 0);
+
+    if (loInclusive > 0)
+      distinctSamples += loInclusive;
+  }
+  else
+  {
+    distinctSamples.set_size(samplesRangeSize);
+    for (size_t i = 0; i < samplesRangeSize; i++)
+      distinctSamples[i] = loInclusive + i;
+  }
+}
+
 } // namespace math
 } // namespace mlpack
 




More information about the mlpack-git mailing list