[mlpack-svn] r10849 - in mlpack/trunk/src/mlpack: methods/gmm methods/hmm methods/hmm/distributions tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Dec 16 02:51:19 EST 2011


Author: rcurtin
Date: 2011-12-16 02:51:19 -0500 (Fri, 16 Dec 2011)
New Revision: 10849

Modified:
   mlpack/trunk/src/mlpack/methods/gmm/gmm.cpp
   mlpack/trunk/src/mlpack/methods/hmm/distributions/discrete_distribution.cpp
   mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp
   mlpack/trunk/src/mlpack/tests/gmm_test.cpp
   mlpack/trunk/src/mlpack/tests/hmm_test.cpp
   mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp
   mlpack/trunk/src/mlpack/tests/tree_test.cpp
Log:
Stop using rand() and use Boost random numbers.


Modified: mlpack/trunk/src/mlpack/methods/gmm/gmm.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/gmm/gmm.cpp	2011-12-16 07:34:10 UTC (rev 10848)
+++ mlpack/trunk/src/mlpack/methods/gmm/gmm.cpp	2011-12-16 07:51:19 UTC (rev 10849)
@@ -33,7 +33,7 @@
 arma::vec GMM::Random() const
 {
   // Determine which Gaussian it will be coming from.
-  double gaussRand = (double) rand() / (double) RAND_MAX;
+  double gaussRand = math::Random();
   size_t gaussian;
 
   double sumProb = 0;

Modified: mlpack/trunk/src/mlpack/methods/hmm/distributions/discrete_distribution.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/hmm/distributions/discrete_distribution.cpp	2011-12-16 07:34:10 UTC (rev 10848)
+++ mlpack/trunk/src/mlpack/methods/hmm/distributions/discrete_distribution.cpp	2011-12-16 07:51:19 UTC (rev 10849)
@@ -16,7 +16,7 @@
 arma::vec DiscreteDistribution::Random() const
 {
   // Generate a random number.
-  double randObs = (double) rand() / (double) RAND_MAX;
+  double randObs = math::Random();
   arma::vec result(1);
 
   double sumProb = 0;

Modified: mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp	2011-12-16 07:34:10 UTC (rev 10848)
+++ mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp	2011-12-16 07:51:19 UTC (rev 10849)
@@ -284,7 +284,7 @@
   stateSequence[0] = startState;
 
   // Choose first emission state.
-  double randValue = (double) rand() / (double) RAND_MAX;
+  double randValue = math::Random();
 
   // We just have to find where our random value sits in the probability
   // distribution of emissions for our starting state.
@@ -294,7 +294,7 @@
   for (size_t t = 1; t < length; t++)
   {
     // First choose the hidden state.
-    randValue = (double) rand() / (double) RAND_MAX;
+    randValue = math::Random();
 
     // Now find where our random value sits in the probability distribution of
     // state changes.

Modified: mlpack/trunk/src/mlpack/tests/gmm_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/gmm_test.cpp	2011-12-16 07:34:10 UTC (rev 10848)
+++ mlpack/trunk/src/mlpack/tests/gmm_test.cpp	2011-12-16 07:51:19 UTC (rev 10849)
@@ -323,7 +323,7 @@
 
   for (size_t i = 0; i < 1500; i++)
   {
-    double randValue = (double) rand() / (double) RAND_MAX;
+    double randValue = math::Random();
 
     if (randValue <= 0.20) // p(d1) = 0.20
       points.col(i) = d1.Random();
@@ -339,7 +339,7 @@
     // plus or minus a little bit of noise.  The base probability (minus the
     // noise) is parameterizable for easy modification of the test.
     double confidence = 0.995;
-    double perturbation = 0.01 * (((double) rand() / (double) RAND_MAX) - 0.5);
+    double perturbation = math::Random(-0.005, 0.005);
 
     if (randValue <= 0.90)
       probabilities(i) = confidence + perturbation;

Modified: mlpack/trunk/src/mlpack/tests/hmm_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/hmm_test.cpp	2011-12-16 07:34:10 UTC (rev 10848)
+++ mlpack/trunk/src/mlpack/tests/hmm_test.cpp	2011-12-16 07:51:19 UTC (rev 10849)
@@ -232,7 +232,7 @@
     for (size_t obs = 0; obs < obsLen; obs++)
     {
       // See if state changed.
-      double r = (double) rand() / (double) RAND_MAX;
+      double r = math::Random();
 
       if (r <= 0.5)
         state = 0;
@@ -240,7 +240,7 @@
         state = 1;
 
       // Now set the observation.
-      r = (double) rand() / (double) RAND_MAX;
+      r = math::Random();
 
       switch (state)
       {
@@ -311,7 +311,7 @@
     states[n].set_size(obsLen);
 
     // Random starting state.
-    states[n][0] = rand() % 3;
+    states[n][0] = math::RandInt(3);
 
     // Random starting observation.
     observations[n].col(0) = emission[states[n][0]].Random();
@@ -320,7 +320,7 @@
     for (size_t t = 1; t < obsLen; t++)
     {
       // Choose random number for state transition.
-      double state = (double) rand() / (double) RAND_MAX;
+      double state = math::Random();
 
       // Decide next state.
       double sumProb = 0;
@@ -436,7 +436,7 @@
   for (int i = 0; i < numSeq; i++)
   {
     // Random starting state.
-    size_t startState = rand() % 4;
+    size_t startState = math::RandInt(4);
 
     hmm.Generate(numObs, sequences[i], states[i], startState);
   }
@@ -517,7 +517,7 @@
   observations.col(0) = g1.Random();
   for (size_t i = 1; i < 1000; i++)
   {
-    double randValue = (double) rand() / (double) RAND_MAX;
+    double randValue = math::Random();
 
     if (randValue > 0.75) // Then we change state.
       classes[i] = (classes[i - 1] + 1) % 2;
@@ -586,7 +586,7 @@
     for (size_t t = 1; t < 1000; t++)
     {
       // Choose the state.
-      double randValue = (double) rand() / (double) RAND_MAX;
+      double randValue = math::Random();
       double probSum = 0;
       for (size_t state = 0; state < 3; state++)
       {
@@ -769,7 +769,7 @@
 
   for (size_t i = 1; i < 1000; i++)
   {
-    double randValue = (double) rand() / (double) RAND_MAX;
+    double randValue = math::Random();
 
     if (randValue <= trans(0, states[i - 1]))
       states[i] = 0;

Modified: mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp	2011-12-16 07:34:10 UTC (rev 10848)
+++ mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp	2011-12-16 07:51:19 UTC (rev 10849)
@@ -43,8 +43,8 @@
   for (size_t elem = 0; elem < points.n_elem; elem++)
   {
     // Max added noise is 0.02.
-    points[elem] += ((double) rand() / (double) INT_MAX) / 50.0;
-    predictors[elem] += ((double) rand() / (double) INT_MAX) / 50.0;
+    points[elem] += math::Random() / 50.0;
+    predictors[elem] += math::Random() / 50.0;
   }
 
   // Generate responses.

Modified: mlpack/trunk/src/mlpack/tests/tree_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/tree_test.cpp	2011-12-16 07:34:10 UTC (rev 10848)
+++ mlpack/trunk/src/mlpack/tests/tree_test.cpp	2011-12-16 07:51:19 UTC (rev 10849)
@@ -353,7 +353,7 @@
 {
   for (int i = 0; i < 50; i++)
   {
-    size_t dim = rand() % 20;
+    size_t dim = math::RandInt(20);
 
     HRectBound<2> a(dim);
     HRectBound<2> b(dim);
@@ -404,7 +404,7 @@
 {
   for (int i = 0; i < 20; i++)
   {
-    size_t dim = rand() % 20;
+    size_t dim = math::RandInt(20);
 
     HRectBound<2> a(dim);
 




More information about the mlpack-svn mailing list