Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Removed incorrect Random usages. #319

Open
wants to merge 1 commit into
base: 3.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/main/java/net/librec/math/algorithm/Randoms.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,7 +42,7 @@ public class ConvMFDocumentProvider {
private int numDoc;

public ConvMFDocumentProvider(@NonNull List<String> documents, @NonNull List<double[]> labelsForSentences) {
this(documents, labelsForSentences, new Random());
this(documents, labelsForSentences, Randoms.randomInstance());
if(documents == null) {
throw new NullPointerException("document");
} else if(labelsForSentences == null) {
Expand Down Expand Up @@ -71,7 +72,7 @@ public ConvMFDocumentProvider(@NonNull List<String> documents, @NonNull List<dou
for(int i = 0; i < documents.size(); this.order[i] = i++) {
}

RandomUtils.shuffleInPlace(this.order, rng);
RandomUtils.shuffleInPlace(this.order, this.rng);
}

}
Expand All @@ -97,7 +98,6 @@ public void reset() {
if(this.rng != null) {
RandomUtils.shuffleInPlace(this.order, this.rng);
}

}

public int totalNumSentences() {
Expand Down