兩個(gè)簡(jiǎn)單的例子,代碼實(shí)現(xiàn)如下:
1、隨機(jī)拆分一個(gè)整數(shù)
public static List<Integer> randomList(int n){ Random rand = new Random(); List<Integer> list = new ArrayList<>(); int i = 0; while (i < n) { int num = rand.nextInt(n); if (!list.contains(num)) { list.add(num); i++; } } return list; }2、從已知列表中隨機(jī)選取不同對(duì)象
public static List<Integer> randomList(int n,int size) { Random rand = new Random(); List<Integer> list = new ArrayList<>(); int i = 1; while (i <= n) { int num = rand.nextInt(size-1) + 1; if (!list.contains(num)) { list.add(num); i++; } } return list; }3、把一個(gè)整數(shù)拆分成不等的幾份
public static List<Integer> randomList(int n, int m){ Random rand = new Random(); List<Integer> list = new ArrayList<>(); int temp = m; for(int i = 0, j; i < n-1; i++){ j = rand.nextInt(temp-1) + 1; temp -= j; list.add(j); if (temp == 1){ break; } } list.add(temp); return list;}例子很簡(jiǎn)單,僅供參考。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)VeVb武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選