[英]Printing the max number from MathRandom generated numbers
My task is to print 1,000,000 random numbers between 0 and 1 with the use of loops. 我的任务是使用循环在0和1之间打印1,000,000个随机数。
I've chosen to use the function Math.random to do this and a for loop to create 1,000,000. 我选择使用函数Math.random进行此操作,并使用for循环创建1,000,000。
Now I need to print the max value of the numbers...but how? 现在我需要打印数字的最大值...但是如何? Is it possible to do it within putting them into an array?
是否可以在将它们放入数组内进行操作?
Here's my code so far: 到目前为止,这是我的代码:
public class GenerateRandomNumbers
{
public static void main(String[]args)
{
for(int i=0; i < 1000000; i++){
System.out.println(Math.random());
}
}
}
You simply need to keep track of the current maximum, and update it accordingly: 您只需要跟踪当前的最大值,并进行相应的更新即可:
public static void main(String[]args) {
double max = 0.0d;
for(int i=0; i < 1000000; i++){
double x = Math.random();
max = Math.max(max,x);
System.out.println(x);
}
System.out.println("The maximum is "+max);
}
The code works as follows: from the specifications of Math.random
, we known that the lowest value is 0.0
, so we first set max
to 0.0d
. 该代码的工作方式如下:根据
Math.random
的规范,我们知道最小值为0.0
,因此我们首先将max
设置为0.0d
。
Next we iterate over the 1000000
samples. 接下来,我们迭代
1000000
样本。 Before printing the value, we first store it in a double x
. 在打印值之前,我们首先将其存储在
double x
。 Next we update the maximum: max
stored the maximum thus far, by calculating the maximum of max
and x
, we include x
as well. 接下来,我们更新最大值:
max
通过计算max
和x
max
存储到目前为止的最大值 ,我们也包括x
。
Finally after the for
loop, we know for sure that max
contains the maximum value encountered, and we can print it (or do other things with it). 最后,在
for
循环之后,我们肯定知道max
包含遇到的最大值,并且我们可以打印它(或使用它做其他事情)。
Ask yourself the question "can I compute the maximum on the fly ?", ie using some induction rule without storing the numbers. 问问自己一个问题“我可以即时计算最大值吗?”,即使用一些归纳规则而不存储数字。
More precisely, assume that you computed the maximum of the N first numbers. 更准确地说,假设您计算了N个第一个数字的最大值。 If you compute the next number, can you get the maximum of the N+1 first ?
如果计算下一个数字,可以先获取N + 1个最大值吗?
The answer is an obvious yes, as "the maximum of the N+1 first numbers is the largest of the N+1th number and the maximum of the N first numbers". 答案显然是肯定的,因为“ N + 1个第一数字的最大值是N + 1个数字的最大值和N个第一数字的最大值”。 In other words, if the new number is larger than the maximum so far, it supersedes it.
换句话说,如果新数字大于到目前为止的最大值,它将取代它。
Also note that the "maximum" of the first number is that number. 另请注意,第一个数字的“最大值”是该数字。
Hence, in pseudo-code, 因此,用伪代码,
Maximum= Random()
for i in range(1, 1000000):
R= Random()
if R > Maximum:
Maximum= R
You can use a slightly simpler variant based on the rule "the maximum of no number is conventionally the smallest possible value", here 0. 您可以根据“无数的最大值通常是最小的可能值”(此处为0)的规则使用稍微简单一些的变体。
Maximum= 0
for i in range(0, 1000000):
R= Random()
if R > Maximum:
Maximum= R
This way of thinking introduces an important concept in programming: the loop invariant. 这种思维方式在编程中引入了一个重要概念:循环不变式。 Throughout execution of the loop, the following condition is maintained: "the variable Maximum holds the largest value among those that have been computed so far".
在循环的整个执行过程中,将保持以下条件:“变量Maximum保持到目前为止已计算出的最大值中的最大值”。 Thinking in terms of invariants helps you in the design of algorithms.
关于不变性的思考可以帮助您设计算法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.