Created
February 9, 2016 06:31
-
-
Save sangcomz/5d20966d936083ee4384 to your computer and use it in GitHub Desktop.
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
import java.util.Random; | |
/** | |
* Created by sangc on 2016-02-09. | |
*/ | |
public class Rand { | |
public static void main(String[] args) { | |
for (int i = 0; i < 100; i++) { | |
getRandom(10); | |
} | |
} | |
public static int getZeroOrOne() { | |
return new Random().nextInt(2); | |
} | |
protected static int getRandom(int i) { | |
String convertBinary = Integer.toBinaryString(i - 1); | |
String randBinary; | |
do { | |
randBinary = ""; | |
for (int j = 0; j < convertBinary.length(); j++) { | |
randBinary += String.valueOf(getZeroOrOne()); | |
} | |
} while (convertBinary.compareTo(randBinary) < 0); | |
return Integer.valueOf(randBinary, 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment