I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 2cb519cd authored by jrandom's avatar jrandom Committed by zzz
Browse files

updated implementation to match the RandomSource javadocs (inclusive of both 0...

updated implementation to match the RandomSource javadocs (inclusive of both 0 and n, unlike SecureRandom, which is inclusive of 0 and exclusive of n)
parent bc46ad43
No related branches found
No related tags found
No related merge requests found
......@@ -31,16 +31,17 @@ public class RandomSource extends SecureRandom {
/**
* According to the java docs (http://java.sun.com/j2se/1.4.1/docs/api/java/util/Random.html#nextInt(int))
* nextInt(n) should return a number between 0 and n inclusive. However, their pseudocode,
* nextInt(n) should return a number between 0 and n (including 0 and excluding n). However, their pseudocode,
* as well as sun's, kaffe's, and classpath's implementation INCLUDES NEGATIVE VALUES.
* WTF. Ok, so we're going to have it return between 0 and n, since thats what
* it has been used for.
* WTF. Ok, so we're going to have it return between 0 and n (including both 0 and n), since
* thats what it has been used for.
*
*/
public int nextInt(int n) {
if (n == 0) return 0;
int val = super.nextInt(n);
int val = super.nextInt(n+1);
if (val < 0) val = 0 - val;
if (val > n) val = val % (n+1);
return val;
}
......@@ -51,7 +52,7 @@ public class RandomSource extends SecureRandom {
public long nextLong(long n) {
long v = super.nextLong();
if (v < 0) v = 0 - v;
if (v > n) v = v % n;
if (v > n) v = v % (n+1);
return v;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment