Skip to content

Commit c7f9f3e

Browse files
committed
revert
1 parent 90f623d commit c7f9f3e

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

src/main/java/de/tilman_neumann/jml/factor/pollardRho/PollardRhoBrentMontgomery64.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -14,14 +14,14 @@
1414
package de.tilman_neumann.jml.factor.pollardRho;
1515

1616
import java.math.BigInteger;
17-
import java.util.Random;
1817

1918
import org.apache.logging.log4j.Logger;
2019
import org.apache.logging.log4j.LogManager;
2120

2221
import de.tilman_neumann.jml.base.Uint128;
2322
import de.tilman_neumann.jml.factor.FactorAlgorithm;
2423
import de.tilman_neumann.jml.gcd.Gcd63;
24+
import de.tilman_neumann.jml.random.Rng;
2525
import de.tilman_neumann.util.Ensure;
2626

2727
/**
@@ -46,7 +46,7 @@ public class PollardRhoBrentMontgomery64 extends FactorAlgorithm {
4646
private static final Logger LOG = LogManager.getLogger(PollardRhoBrentMontgomery64.class);
4747
private static final boolean DEBUG = false;
4848

49-
private static final Random RNG = new Random(); // the numbers produced by Random.nextLong(bound) seem to have very good random number qualities
49+
private static final Rng RNG = new Rng();
5050

5151
// The reducer R is 2^64, but the only constant still required is the half of it.
5252
private static final long R_HALF = 1L << 63;

src/main/java/de/tilman_neumann/jml/factor/pollardRho/PollardRhoBrentMontgomery64MH.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -14,14 +14,14 @@
1414
package de.tilman_neumann.jml.factor.pollardRho;
1515

1616
import java.math.BigInteger;
17-
import java.util.Random;
1817

1918
import org.apache.logging.log4j.Logger;
2019
import org.apache.logging.log4j.LogManager;
2120

2221
import de.tilman_neumann.jml.base.Uint128;
2322
import de.tilman_neumann.jml.factor.FactorAlgorithm;
2423
import de.tilman_neumann.jml.gcd.Gcd63;
24+
import de.tilman_neumann.jml.random.Rng;
2525
import de.tilman_neumann.util.Ensure;
2626

2727
/**
@@ -50,7 +50,7 @@ public class PollardRhoBrentMontgomery64MH extends FactorAlgorithm {
5050
private static final Logger LOG = LogManager.getLogger(PollardRhoBrentMontgomery64MH.class);
5151
private static final boolean DEBUG = false;
5252

53-
private static final Random RNG = new Random(); // the numbers produced by Random.nextLong(bound) seem to have very good random number qualities
53+
private static final Rng RNG = new Rng();
5454

5555
// The reducer R is 2^64, but the only constant still required is the half of it.
5656
private static final long R_HALF = 1L << 63;

src/main/java/de/tilman_neumann/jml/factor/pollardRho/PollardRhoBrentMontgomery64MHInlined.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -14,14 +14,14 @@
1414
package de.tilman_neumann.jml.factor.pollardRho;
1515

1616
import java.math.BigInteger;
17-
import java.util.Random;
1817

1918
import org.apache.logging.log4j.Logger;
2019
import org.apache.logging.log4j.LogManager;
2120

2221
import de.tilman_neumann.jml.base.Uint128;
2322
import de.tilman_neumann.jml.factor.FactorAlgorithm;
2423
import de.tilman_neumann.jml.gcd.Gcd63;
24+
import de.tilman_neumann.jml.random.Rng;
2525
import de.tilman_neumann.util.Ensure;
2626

2727
/**
@@ -50,7 +50,7 @@ public class PollardRhoBrentMontgomery64MHInlined extends FactorAlgorithm {
5050
private static final Logger LOG = LogManager.getLogger(PollardRhoBrentMontgomery64MHInlined.class);
5151
private static final boolean DEBUG = false;
5252

53-
private static final Random RNG = new Random(); // the numbers produced by Random.nextLong(bound) seem to have very good random number qualities
53+
private static final Rng RNG = new Rng();
5454

5555
// The reducer R is 2^64, but the only constant still required is the half of it.
5656
private static final long R_HALF = 1L << 63;

src/main/java/de/tilman_neumann/jml/random/Rng.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,33 @@ public Rng(long seed) {
5757
* @return random int in the desired range
5858
*/
5959
public int nextInt(int minValue, int maxValue) {
60-
return minValue + nextInt(maxValue - minValue);
60+
int diff = maxValue - minValue;
61+
if (diff < 1) throw new IllegalArgumentException("maxValue=" + maxValue + " must be bigger than minValue=" + minValue);
62+
int rand = nextInt(diff); // 0...(diff-1)
63+
return minValue + rand;
64+
}
65+
66+
/**
67+
* Creates a random long from the uniform distribution U[0, maxValue-1].
68+
* The only requirement is maxValue > 0.
69+
* @param maxValue
70+
* @return random long in the desired range
71+
*/
72+
public long nextLong(long maxValue) {
73+
if (maxValue < 1) {
74+
throw new IllegalArgumentException("maxValue = " + maxValue + " is not positive");
75+
}
76+
77+
long rand = nextLong(); // Long.MIN_VALUE...Long.MAX_VALUE
78+
long normalized; // shall become 0...(max-1)
79+
if (rand >= 0) {
80+
normalized = rand % maxValue;
81+
} else if (rand > Long.MIN_VALUE) {
82+
normalized = (-rand) % maxValue;
83+
} else { // special treatment because -Long.MIN_VALUE == Long.MIN_VALUE
84+
normalized = Long.MAX_VALUE % maxValue;
85+
}
86+
return normalized;
6187
}
6288

6389
/**
@@ -68,7 +94,10 @@ public int nextInt(int minValue, int maxValue) {
6894
* @return random long in the desired range
6995
*/
7096
public long nextLong(long minValue, long maxValue) {
71-
return minValue + nextLong(maxValue - minValue);
97+
long diff = maxValue - minValue;
98+
if (diff < 1) throw new IllegalArgumentException("maxValue=" + maxValue + " must be bigger than minValue=" + minValue);
99+
long rand = nextLong(diff); // 0...(diff-1)
100+
return minValue + rand;
72101
}
73102

74103
/**

0 commit comments

Comments
 (0)