Skip to content

Introduced protections against predictable RNG abuse #15

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

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.langchain4j.internal;

import java.security.SecureRandom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -143,7 +144,7 @@ public double rawDelayMs(int attempt) {
* @return The jitter delay in milliseconds.
*/
public int jitterDelayMillis(int attempt) {
Random rand = new Random();
Random rand = new SecureRandom();
double delay = rawDelayMs(attempt);
double jitter = delay * jitterScale;
return (int) (delay + rand.nextInt((int) jitter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.output.TokenUsage;
import java.security.SecureRandom;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -96,7 +97,7 @@ private static List<TextSegment> createRandomSegments(int count, int maxLength)
for (int i = 0; i < count; i++) {
StringBuilder sb = new StringBuilder();
do {
String nextWord = words[new Random().nextInt(words.length)];
String nextWord = words[new SecureRandom().nextInt(words.length)];
if (sb.length() + nextWord.length() + 1 < maxLength) {
sb.append(nextWord).append(" ");
} else break;
Expand Down Expand Up @@ -162,4 +163,4 @@ void testBatchingEmbeddingsWithMaxSet() {

assertThat(embeddings.size()).isEqualTo(1234);
}
}
}