简体   繁体   English

从MathRandom生成的数字中打印最大数字

[英]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通过计算maxx 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.

相关问题 无法从20个随机生成的数字中打印最大的数组数字 - Having trouble in printing the largest array number from the 20 randomly generated numbers 如何从给定的四个数字中生成一个唯一的数字,以及如何从生成的唯一数字中获取这些给定的数字? - How to generate an unique number from a given four numbers and getting these given numbers back from generated unique number? 使用 Java Streams 从字符串数列表中查找最大数 - Find Max number from List of String numbers using Java Streams 在数字字符串中查找最大数字 - Find Max Number in String of Numbers 当从键盘输入 10 个数字时,打印 while 循环中的最大数字 - printing the largest number from a while loop, when 10 numbers are inputted from the keyboard 通过二维数组中随机生成的数字打印字符数 - Printing the number of char's by a randomly generated number in a 2D Array 随机数生成和生成数的排除 - random number generation and exclusion of the generated numbers 从带有循环的int值中打印字符,每行输出中具有所需数量的数字 - Printing a character from an int value with a loop with a desired number of numbers per line of output 使用数字的位数打印一个范围内的所有回文数字 - Printing All Palindrome Numbers Within A Range Using The Number Of Digits Of The Numbers 如何从用户那里获得10个数字并打印出两个最大数字 - How To Get 10 Number From The User And Print Out The Two Max Numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM