點樣令java random number random d
本帖最後由 twaiho2003 於 2016-6-10 18:29 編輯
我發覺JAVA random number 成日gen好接近既數字,
所以我做左個實驗, 抽4個 9 000 000 以內既數字, 但我發覺成日有相同既最大位數 或者好接近既最大位數
例如,
1 xxx xxx, 1 xxx xxx, 8 xxx xxx, 9 xxx xxx
5 xxx xxx, 5 xxx xxx, 6 xxx xxx, 6 xxx xxx
如果按機率, 我抽一個數字, 例如 1 xxx xxx, 再抽多3個, 有相同最大位數既機率應該係 1/9 + 1/9 + 1/9 = 1/3
(有錯請指教)
如果我試100次, 照計得33次左右出現相同最大位數,但結果成日多個50次,
改做 1 000 000 000次, 不斷出result 5 390 xxx 次- public static void main(String[] args) {
- Random random = new Random();
- int repeat_count = 0;
- Set<Integer> num_set = new HashSet<>();
- for (int times=0;times<1000;times++) {
- for (int i=0;i<4;i++) {
- int num = random.nextInt(9000000);
- num_set.add(num/1000000);
- System.out.println(num);
- }
- if (num_set.size() < 4) {
- repeat_count++;
- System.out.println("repeated");
- }
- num_set.clear();
- System.out.println("");
- }
- System.out.println("Repeat Count:" + repeat_count);
- }
複製代碼 |
|
|