Skip to content

Instantly share code, notes, and snippets.

@rankun203
Last active August 29, 2015 14:02
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 rankun203/af1cce028ad276979bdb to your computer and use it in GitHub Desktop.
Save rankun203/af1cce028ad276979bdb to your computer and use it in GitHub Desktop.
import java.util.Arrays;
/**
* Created on 14-6-18
* output:
50058:49942
So, get0or1 works fine.
[12674, 12405, 12604, 12519, 12321, 12520, 12528, 12429]
* @author rankun203@gmail.com
*/
public class Random1to8 {
public static void main(String[] args) {
int lessPart = 0;
int morePart = 0;
for (int i = 0; i < 100000; i++) {
switch (get0or1()) {
case 0:
++lessPart;
break;
case 1:
++morePart;
break;
}
}
System.out.println(lessPart + ":" + morePart);
System.out.println("So, get0or1 works fine.");
int[] nums = new int[8];
for (int i = 0; i < 100000; i++) {
nums[get1to8() - 1]++;
}
System.out.println(Arrays.toString(nums));
}
/*************Random get 1 to 8***************/
public static int get1to8() {
return get0or1() + get0or1() * 2 + get0or1() * 4 + 1;
}
/********************End**********************/
public static int get0or1() {
return Math.random() > 0.5 ? 1 : 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment