Skip to content

Instantly share code, notes, and snippets.

@KengoTODA
Created February 27, 2011 23:20
Show Gist options
  • Save KengoTODA/846687 to your computer and use it in GitHub Desktop.
Save KengoTODA/846687 to your computer and use it in GitHub Desktop.
package jp.skypencil.misc.random;
import java.util.Random;
public class EndlessRetryer {
private final Random seed = new Random();
public int random(int min, int max) {
final int width = max - min;
if (min < 0 || width < 0) throw new IllegalArgumentException();
int random;
do {
random = 0;
int bit = 1;
while (0 < bit && bit <= width) {
if (seed.nextBoolean()) random |= bit;
bit <<= 1;
}
} while (random > width);
return random + min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment