This method randomly increments or decrements (with equal probability) until either -3 or 3 are reached. I'd like to maintain printing the value of pos
on each iteration, and printing the largest positive number max
reached at the end of the loop. How can I improve this method using the Random()
function? I know this sounds pretty ambiguous, but I'd like to know if I can make rand.nextInt(2) == 0
an easier condition to understand? Any suggestions/improvements are welcome!
public static void randomWalk() {
Random rand = new Random();
int pos = 0;
int max = 0;
System.out.println("position = " + pos);
while (pos > -3 && pos < 3) {
if (rand.nextInt(2) == 0) {
pos++;
if (pos > 0) {
max = pos;
}
} else {
pos--;
}
System.out.println("position = " + (int) pos);
}
System.out.println("max position = " + max);
}
2 Answers 2
There's a minor bug there:
if (pos > 0) {
max = pos;
}
should test pos > max
instead.
Use
max = Math.max(max, pos)
to make it shorter.
I'd like to know if I can make rand.nextInt(2) == 0 an easier condition to understand?
Use rand.nextBoolean()
. For a probability of exactly 50%, it's perfect.
For other probabilities, you can use
rand.nextDouble() < probability
Another possibility is simply
pos += 2 * rand.nextInt(2) - 1;
max = Math.max(max, pos);
or
pos += rand.nextBoolean() ? 1 : -1;
max = Math.max(max, pos);
or
private static final int[] DELTAS = {+1, -1};
pos += DELTAS[rand.nextInt(DELTAS.length)];
max = Math.max(max, pos);
without any conditionals. You may consider it tricky, but it isn't. Obviously, my last solution shouldn't be used for such a simple case.
Concerning the computation of max
it may be slightly less efficient since it gets executed even if the value decreases. But this doesn't matter unless you need to optimize heavily (your print
is many orders of magnitude slower than this).
-
\$\begingroup\$ Thanks! I'm glad there is an easier way to determine probability, and that gives me a different perspective on what a boolean can be, rather than just true or false. \$\endgroup\$cody.codes– cody.codes2015年06月30日 01:05:48 +00:00Commented Jun 30, 2015 at 1:05
Expression
pos > -3 && pos < 3
Can be
Math.abs(pos) < 3
Random choice
Java Random
had method for sampling boolean values nextBoolean
Bug
This line
if (pos > 0) {
do not give you
the largest positive number max reached at the end of the loop
Improvement
You don't need to make loop at all. You need random generator that will provide 2 values at each iteration:
- max positive number
- 3 or -3
This can be done using random in following way
long sample = Math.abs(rand.nextLong());
int pos, max;
if (sample % 2 == 0) {
pos = 3;
max = 3;
} else {
pos = -3;
max = int(sample % 3);
}
-
\$\begingroup\$ Concerning
pos
, you're right. Concerningmax
, you're most probably wrong as not all values are equiprobable. Even if they were, you're missing the casemax==3
. Concerning the pair(pos, max)
, as the probabilities are'nt independent, for examplepos==-3
andmax==2
is rather improbable. Andpos==3
andmax<3
is plain impossible. \$\endgroup\$maaartinus– maaartinus2015年06月27日 19:45:06 +00:00Commented Jun 27, 2015 at 19:45 -
\$\begingroup\$ @maaartinus
max
never be 3, read TC's code. I'm so sorry I can't downvote this comment. pastebin.com/nnyTp0CK \$\endgroup\$outoftime– outoftime2015年06月27日 20:54:39 +00:00Commented Jun 27, 2015 at 20:54 -
3\$\begingroup\$ Why shouldn't it be 3. First gets
pos
incremented to 3, thenmax
computed, and then the loop exits. However, it's all irrelevant, as you can't shortcut themax
computation to something that trivial. -1 This simply can't work. \$\endgroup\$maaartinus– maaartinus2015年06月27日 21:10:30 +00:00Commented Jun 27, 2015 at 21:10 -
\$\begingroup\$ @maaartinus Now I see problem with
max
value. Fixed. \$\endgroup\$outoftime– outoftime2015年06月27日 21:17:41 +00:00Commented Jun 27, 2015 at 21:17 -
\$\begingroup\$ @maaartinus but I'm not agree that part of uniform distribution can be not uniform. This relate to
max
andpos
values, as they are both parts of uniformly distributed values ofsample
\$\endgroup\$outoftime– outoftime2015年06月27日 21:21:01 +00:00Commented Jun 27, 2015 at 21:21