Created
February 27, 2011 23:20
-
-
Save KengoTODA/846687 to your computer and use it in GitHub Desktop.
inspired by http://d.hatena.ne.jp/seizans/20110227
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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