Skip to content

Commit 90f623d

Browse files
committed
use java.util.Random.nextLong(bound) - it's good!
1 parent 835a337 commit 90f623d

File tree

4 files changed

+11
-40
lines changed

4 files changed

+11
-40
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-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 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;
1718

1819
import org.apache.logging.log4j.Logger;
1920
import org.apache.logging.log4j.LogManager;
2021

2122
import de.tilman_neumann.jml.base.Uint128;
2223
import de.tilman_neumann.jml.factor.FactorAlgorithm;
2324
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 Rng RNG = new Rng();
49+
private static final Random RNG = new Random(); // the numbers produced by Random.nextLong(bound) seem to have very good random number qualities
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-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 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;
1718

1819
import org.apache.logging.log4j.Logger;
1920
import org.apache.logging.log4j.LogManager;
2021

2122
import de.tilman_neumann.jml.base.Uint128;
2223
import de.tilman_neumann.jml.factor.FactorAlgorithm;
2324
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 Rng RNG = new Rng();
53+
private static final Random RNG = new Random(); // the numbers produced by Random.nextLong(bound) seem to have very good random number qualities
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-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 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;
1718

1819
import org.apache.logging.log4j.Logger;
1920
import org.apache.logging.log4j.LogManager;
2021

2122
import de.tilman_neumann.jml.base.Uint128;
2223
import de.tilman_neumann.jml.factor.FactorAlgorithm;
2324
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 Rng RNG = new Rng();
53+
private static final Random RNG = new Random(); // the numbers produced by Random.nextLong(bound) seem to have very good random number qualities
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: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,7 @@ public Rng(long seed) {
5757
* @return random int in the desired range
5858
*/
5959
public int nextInt(int minValue, int maxValue) {
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;
60+
return minValue + nextInt(maxValue - minValue);
8761
}
8862

8963
/**
@@ -94,10 +68,7 @@ public long nextLong(long maxValue) {
9468
* @return random long in the desired range
9569
*/
9670
public long nextLong(long minValue, long maxValue) {
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;
71+
return minValue + nextLong(maxValue - minValue);
10172
}
10273

10374
/**

0 commit comments

Comments
 (0)