# Java概率抽獎算法實現，歡迎拍磚

## 算法實現

``````// 入參示例：int[] array = {30, 40, 20, 10};
private static int getIndex(int[] array) {
int num = new Random().nextInt(100);
for (int i = 0; i < array.length; i++) {
if (num < array[i]) {
return i;
}
num -= array[i];
}
return -1;
}
``````

## 如何衡量

``````// 計算概率，測試10000次
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < 10000; i++) {
// 單次獲取概率結果
int index = getIndex(array);
// System.out.printf("中獎啦! 概率: %d, Index: %d%n", array[index], index);

Integer value = map.getOrDefault(array[index], 0);
map.put(array[index], value + 1);
}

System.out.println("----------------------------------------------");
System.out.printf("看看結果吧! 概率: %s%n", map);
System.out.println("----------------------------------------------");
``````

``````----------------------------------------------

----------------------------------------------

----------------------------------------------

----------------------------------------------

----------------------------------------------

----------------------------------------------

----------------------------------------------
``````

## 健壯性

1. 總概率不滿100%時，補全數組；
2. 計算中獎的時候，再捨去補全的最後一組（當然，如果沒補就不用捨去）。

``````package cn.eyeo.mall.start.lottery;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* 抽獎測試類
*
* @author <a href="mailto:[email protected]">amos.wang</a>
* @date 2023/3/27
*/
public class LotteryDrawTests {

private static final int TOTAL_PROBABILITY = 100;

@Test
public void lottery() {
int[] array = {30, 40, 20};

probability(array);
}

private static void probability(int[] source) {
int sum = Arrays.stream(source).sum();
if (sum > TOTAL_PROBABILITY) {
throw new IllegalArgumentException("總概率不能>100");
}

System.out.println("----------------------------------------------");
System.out.println("原始數據: " + Arrays.toString(source));

// 不滿100%則補全
int[] array = source;
if (sum < TOTAL_PROBABILITY) {
array = new int[source.length + 1];
System.arraycopy(source, 0, array, 0, source.length);
array[source.length] = TOTAL_PROBABILITY - sum;
}
System.out.println("補全後的數據: " + Arrays.toString(array));

// 計算概率，測試10000次
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < 10000; i++) {
// 單次獲取概率結果
int index = getIndex(array);
if (index < source.length) {
// System.out.printf("中獎啦! 概率: %d, Index: %d%n", array[index], index);

Integer value = map.getOrDefault(array[index], 0);
map.put(array[index], value + 1);
}
}

System.out.println("----------------------------------------------");
System.out.printf("看看結果吧! 概率: %s%n", map);
}

private static int getIndex(int[] array) {
int num = new Random().nextInt(TOTAL_PROBABILITY);
for (int i = 0; i < array.length; i++) {
if (num < array[i]) {
return i;
}
num -= array[i];
}
return -1;
}

}
``````