From ea60f62286ff51aacdbe1420392c282dcddf5703 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 22 Feb 2014 13:04:44 -0500 Subject: [PATCH] Fixed a bug in randomize_samples(). It forced each element of the input arrays to move to a new position but was random amongst all permutations with such moves. However, this isn't really fully random so this function has been fixed so it does exactly what the spec says it should. --- dlib/svm/svm.h | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/dlib/svm/svm.h b/dlib/svm/svm.h index 13ba78dc8..f3899fd01 100644 --- a/dlib/svm/svm.h +++ b/dlib/svm/svm.h @@ -954,11 +954,8 @@ namespace dlib long n = t.size()-1; while (n > 0) { - // put a random integer into idx - unsigned long idx = r.get_random_32bit_number(); - - // make idx be less than n - idx %= n; + // pick a random index to swap into t[n] + const unsigned long idx = r.get_random_32bit_number()%(n+1); // swap our randomly selected index into the n position exchange(t(idx), t(n)); @@ -996,11 +993,8 @@ namespace dlib long n = t.size()-1; while (n > 0) { - // put a random integer into idx - unsigned long idx = r.get_random_32bit_number(); - - // make idx be less than n - idx %= n; + // pick a random index to swap into t[n] + const unsigned long idx = r.get_random_32bit_number()%(n+1); // swap our randomly selected index into the n position exchange(t[idx], t[n]); @@ -1055,11 +1049,8 @@ namespace dlib long n = t.size()-1; while (n > 0) { - // put a random integer into idx - unsigned long idx = r.get_random_32bit_number(); - - // make idx be less than n - idx %= n; + // pick a random index to swap into t[n] + const unsigned long idx = r.get_random_32bit_number()%(n+1); // swap our randomly selected index into the n position exchange(t(idx), t(n)); @@ -1093,11 +1084,8 @@ namespace dlib long n = t.size()-1; while (n > 0) { - // put a random integer into idx - unsigned long idx = r.get_random_32bit_number(); - - // make idx be less than n - idx %= n; + // pick a random index to swap into t[n] + const unsigned long idx = r.get_random_32bit_number()%(n+1); // swap our randomly selected index into the n position exchange(t[idx], t[n]); @@ -1144,11 +1132,8 @@ namespace dlib long n = t.size()-1; while (n > 0) { - // put a random integer into idx - unsigned long idx = r.get_random_32bit_number(); - - // make idx be less than n - idx %= n; + // pick a random index to swap into t[n] + const unsigned long idx = r.get_random_32bit_number()%(n+1); // swap our randomly selected index into the n position exchange(t(idx), t(n)); @@ -1171,11 +1156,8 @@ namespace dlib long n = t.size()-1; while (n > 0) { - // put a random integer into idx - unsigned long idx = r.get_random_32bit_number(); - - // make idx be less than n - idx %= n; + // pick a random index to swap into t[n] + const unsigned long idx = r.get_random_32bit_number()%(n+1); // swap our randomly selected index into the n position exchange(t[idx], t[n]);