Skip to content

Instantly share code, notes, and snippets.

@sangcomz
Created February 9, 2016 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sangcomz/5d20966d936083ee4384 to your computer and use it in GitHub Desktop.
Save sangcomz/5d20966d936083ee4384 to your computer and use it in GitHub Desktop.
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