Last active
August 29, 2015 14:02
-
-
Save rankun203/af1cce028ad276979bdb to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Arrays; | |
/** | |
* Created on 14-6-18 | |
* output: | |
50058:49942 | |
So, get0or1 works fine. | |
[12674, 12405, 12604, 12519, 12321, 12520, 12528, 12429] | |
* @author [email protected] | |
*/ | |
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