我想知道如何编写一个函数来生成0到max - 1范围内的随机数,并返回一个数组计数,其中元素计数[i]是生成值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];
}
答案 0 :(得分:2)
public static int[] randomExperiment(int max, int iters)
{
Random randomGenerator = new Random();
int[] counts = new int [max];
for(int i = 0; i < iters; i++)
{
int randomInt = randomGenerator.nextInt(max);
counts[randomInt]++;
}
return counts;
}
对Gendame和Titus的称赞