Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
你如何找到一组随机数的平均值。例如,您将如何找到平均值,假设您提示用户他们想要生成随机数的次数,他们输入了 9,然后您使用 来生成随机数random = randlist.nextInt(100) + 1;?我知道您通过将数字的总和除以有多少数字来找到平均值,但是在这种情况下,您如何找到由随机方法生成的数字并将它们加在一起并除以 9?
random = randlist.nextInt(100) + 1;
和:
double avg = 0.0; for (int i = 0; i < nTimes; i++) { avg += randlist.nextInt(100) + 1; } avg /= nTimes; //This is the final average