0
我想知道如何編寫一個函數,生成iters隨機數字,範圍從0到max - 1,並返回元素計數數組[i]是我生成價值的次數。以下是我迄今爲止所寫的內容。編寫一個函數,計算每個數字隨機生成的次數
public static int[] randomExperiment(int max, int iters)
{
Random randomGenerator = new Random();
for(int i = 0; i < iters; i += 1)
{
int randomInt = randomGenerator.nextInt(max - 1);
}
int[] counts = new int [iters];
}
什麼是你的問題? – Gendarme
對不起,我沒有那麼清楚。我如何計算每個值的隨機生成數量? – nshull16
只需創建一個新的數組'int [] counter = new int [max]'並在循環內部執行類似'counter [randomInt] ++'的操作。 – Titus