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

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

clarify the nextInt/nextLong boundaries (thanks oOo)

parent 53c7ff14
No related branches found
No related tags found
No related merge requests found
...@@ -138,8 +138,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelClientBase implements Runnable ...@@ -138,8 +138,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelClientBase implements Runnable
return null; return null;
} }
int index = I2PAppContext.getGlobalContext().random().nextInt(size); int index = I2PAppContext.getGlobalContext().random().nextInt(size);
if (index >= size) index = size - 1;
if (index < 0) return null;
String proxy = (String)proxyList.get(index); String proxy = (String)proxyList.get(index);
return proxy; return proxy;
} }
......
...@@ -33,7 +33,7 @@ public class RandomSource extends SecureRandom { ...@@ -33,7 +33,7 @@ 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)) * 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 (including 0 and excluding n). 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. * 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 * WTF. Ok, so we're going to have it return between 0 and n (including 0, excluding n), since
* thats what it has been used for. * thats what it has been used for.
* *
*/ */
...@@ -41,18 +41,18 @@ public class RandomSource extends SecureRandom { ...@@ -41,18 +41,18 @@ public class RandomSource extends SecureRandom {
if (n == 0) return 0; if (n == 0) return 0;
int val = super.nextInt(n); int val = super.nextInt(n);
if (val < 0) val = 0 - val; if (val < 0) val = 0 - val;
if (val > n) val = val % n; if (val >= n) val = val % n;
return val; return val;
} }
/** /**
* Like the modified nextInt, nextLong(n) returns a random number from 0 through n, * Like the modified nextInt, nextLong(n) returns a random number from 0 through n,
* inclusive. * including 0, excluding n.
*/ */
public long nextLong(long n) { public long nextLong(long n) {
long v = super.nextLong(); long v = super.nextLong();
if (v < 0) v = 0 - v; if (v < 0) v = 0 - v;
if (v > n) v = v % n; if (v >= n) v = v % n;
return v; return v;
} }
......
...@@ -67,7 +67,7 @@ class DataPublisherJob extends JobImpl { ...@@ -67,7 +67,7 @@ class DataPublisherJob extends JobImpl {
// if there's nothing we *need* to send, only send 10% of the time // if there's nothing we *need* to send, only send 10% of the time
if (explicit.size() <= 0) { if (explicit.size() <= 0) {
if (getContext().random().nextInt(9) <= 8) if (getContext().random().nextInt(10) > 0)
return toSend; return toSend;
} }
......
...@@ -71,7 +71,7 @@ class TunnelPoolManagerJob extends JobImpl { ...@@ -71,7 +71,7 @@ class TunnelPoolManagerJob extends JobImpl {
built = true; built = true;
} else { } else {
// 10% chance of building a new tunnel // 10% chance of building a new tunnel
if (getContext().random().nextInt(9) > 0) { if (getContext().random().nextInt(10) > 0) {
// all good, no need for more inbound tunnels // all good, no need for more inbound tunnels
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Sufficient inbound tunnels (" + curFreeInboundTunnels + ")"); _log.debug("Sufficient inbound tunnels (" + curFreeInboundTunnels + ")");
...@@ -93,7 +93,7 @@ class TunnelPoolManagerJob extends JobImpl { ...@@ -93,7 +93,7 @@ class TunnelPoolManagerJob extends JobImpl {
built = true; built = true;
} else { } else {
// 10% chance of building a new tunnel // 10% chance of building a new tunnel
if (getContext().random().nextInt(9) > 0) { if (getContext().random().nextInt(10) > 0) {
// all good, no need for more outbound tunnels // all good, no need for more outbound tunnels
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Sufficient outbound tunnels (" + curOutboundTunnels + ")"); _log.debug("Sufficient outbound tunnels (" + curOutboundTunnels + ")");
......
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