diff --git a/core/src/main/java/net/librec/math/algorithm/Randoms.java b/core/src/main/java/net/librec/math/algorithm/Randoms.java index 882ef8b0c..9c2d62ee5 100644 --- a/core/src/main/java/net/librec/math/algorithm/Randoms.java +++ b/core/src/main/java/net/librec/math/algorithm/Randoms.java @@ -46,6 +46,10 @@ public static void seed(long seed) { r = new Random(seed); } + public static Random randomInstance() { + return r; + } + /** * Random generate an integer in [min, max) * diff --git a/core/src/main/java/net/librec/recommender/cf/ranking/ItemBigramRecommender.java b/core/src/main/java/net/librec/recommender/cf/ranking/ItemBigramRecommender.java index 10cac0512..736d00e67 100644 --- a/core/src/main/java/net/librec/recommender/cf/ranking/ItemBigramRecommender.java +++ b/core/src/main/java/net/librec/recommender/cf/ranking/ItemBigramRecommender.java @@ -171,7 +171,7 @@ protected void setup() throws LibrecException { for (int itemIdxIndex = 0; itemIdxIndex < itemIdxList.size(); itemIdxIndex++) { int itemIdx = itemIdxList.get(itemIdxIndex); - int topicIdx = (int) (Math.random() * numTopics); + int topicIdx = (int) (Randoms.uniform() * numTopics); topicAssignments.put(userIdx, itemIdx, topicIdx); userTopicNumbers.plus(userIdx, topicIdx, 1.0); diff --git a/core/src/main/java/net/librec/recommender/cf/ranking/NMFItemItemRecommender.java b/core/src/main/java/net/librec/recommender/cf/ranking/NMFItemItemRecommender.java index 7691e11dc..d919c0a20 100644 --- a/core/src/main/java/net/librec/recommender/cf/ranking/NMFItemItemRecommender.java +++ b/core/src/main/java/net/librec/recommender/cf/ranking/NMFItemItemRecommender.java @@ -20,6 +20,7 @@ import com.google.common.collect.BiMap; import net.librec.common.LibrecException; +import net.librec.math.algorithm.Randoms; import net.librec.math.structure.SequentialSparseVector; import net.librec.recommender.MatrixFactorizationRecommender; @@ -146,11 +147,9 @@ protected void setup() throws LibrecException { private void initMatrix(double[][] m) { double initValue = 1d / (numItems * 2d); - Random random = new Random(123456789L); - for (int i = 0; i < m.length; i++){ for (int j = 0; j < m[i].length; j++){ - m[i][j] = (random.nextDouble() + 1) * initValue; + m[i][j] = (Randoms.uniform() + 1) * initValue; } } } diff --git a/core/src/main/java/net/librec/recommender/cf/ranking/PNMFRecommender.java b/core/src/main/java/net/librec/recommender/cf/ranking/PNMFRecommender.java index e94165896..8d6c73d85 100644 --- a/core/src/main/java/net/librec/recommender/cf/ranking/PNMFRecommender.java +++ b/core/src/main/java/net/librec/recommender/cf/ranking/PNMFRecommender.java @@ -20,6 +20,7 @@ import com.google.common.collect.BiMap; import net.librec.common.LibrecException; +import net.librec.math.algorithm.Randoms; import net.librec.math.structure.SequentialSparseVector; import net.librec.recommender.MatrixFactorizationRecommender; @@ -118,11 +119,9 @@ protected void setup() throws LibrecException { private void initMatrix(double[][] m) { double initValue = 1d / (numItems * 2d); - Random random = new Random(123456789L); - for (int i = 0; i < m.length; i++){ for (int j = 0; j < m[i].length; j++){ - m[i][j] = (random.nextDouble() + 1) * initValue; + m[i][j] = (Randoms.uniform() + 1) * initValue; } } } diff --git a/core/src/main/java/net/librec/recommender/cf/rating/RBMRecommender.java b/core/src/main/java/net/librec/recommender/cf/rating/RBMRecommender.java index fdb35829b..e0b94d3aa 100644 --- a/core/src/main/java/net/librec/recommender/cf/rating/RBMRecommender.java +++ b/core/src/main/java/net/librec/recommender/cf/rating/RBMRecommender.java @@ -151,7 +151,7 @@ protected void setup() throws LibrecException { } for (int k = 0; k < softmax; k++) { if (mtot == 0) { - visbiases[i][k] = new Random().nextDouble() * 0.001; + visbiases[i][k] = Randoms.uniform() * 0.001; } else { visbiases[i][k] = Math.log(((double) moviecount[i][k]) / ((double) mtot)); } diff --git a/core/src/main/java/net/librec/recommender/content/ConvMFDocumentProvider.java b/core/src/main/java/net/librec/recommender/content/ConvMFDocumentProvider.java index 68a185697..f17336da0 100644 --- a/core/src/main/java/net/librec/recommender/content/ConvMFDocumentProvider.java +++ b/core/src/main/java/net/librec/recommender/content/ConvMFDocumentProvider.java @@ -18,6 +18,7 @@ package net.librec.recommender.content; import lombok.NonNull; +import net.librec.math.algorithm.Randoms; import org.datavec.api.util.RandomUtils; import org.nd4j.linalg.primitives.Pair; @@ -41,7 +42,7 @@ public class ConvMFDocumentProvider { private int numDoc; public ConvMFDocumentProvider(@NonNull List documents, @NonNull List labelsForSentences) { - this(documents, labelsForSentences, new Random()); + this(documents, labelsForSentences, Randoms.randomInstance()); if(documents == null) { throw new NullPointerException("document"); } else if(labelsForSentences == null) { @@ -71,7 +72,7 @@ public ConvMFDocumentProvider(@NonNull List documents, @NonNull List