C#:在1-1000之间生成100个随机数并输出最大值

时间:2019-02-23 05:27:43

标签: c# random generator

我对编码非常陌生,只是无法绕着Loops / Arrays / Randoms。我理解这个概念,但是在应用它时,我只是迷路了。

在这里,我尝试生成1-1000之间的100个随机数,并且必须输出最大值。到目前为止,这是我的代码:

Random rnd = new Random();
int nums = rnd.Next(0, 1001);
for (int i = 1; i <= 100; i++)
{

}
Console.WriteLine(nums);
Console.ReadLine(); 

只给我一个号码。 :( 非常感谢您的帮助!

谢谢!

5 个答案:

答案 0 :(得分:1)

您可以将生成的随机数累加到数组中,然后使用数组的Max函数来找到最大值

class Program
{
    public static void Main(string[] args)
    {
        Random rnd = new Random();
        int[] intArr = new int[100];

        for (int i = 0; i < intArr.Length; i++)
        {
            int num = rnd.Next(1, 1000);
            intArr[i] = num;
            Console.WriteLine(num);
        }

        Console.WriteLine();
        int maxNum = intArr.Max();
        Console.WriteLine("The max num is:" + maxNum);
        Console.ReadLine();
    }
}

Click to watch online demo

答案 1 :(得分:0)

您需要在循环内调用rnd.Next()。

Random rnd = new Random();
for (int i = 1; i <= 100; i++)
{
    int nums = rnd.Next(0, 1001);
    Console.WriteLine(nums);
}

Console.ReadLine(); 

答案 2 :(得分:0)

我不确定,你这样问吗?

Random random = new Random();

int[] nums = new int[100];

// when for loop ends, nums are full of 100 numbers
for (int i = 0; i < nums.Length; i++)
{
    int newNum = random.Next(1, 1000);

    // show every number
    Console.WriteLine(newNum);

    nums[i] = newNum;
}

// get the max number
var maxNum = nums.Max();

Console.WriteLine(maxNum);

答案 3 :(得分:0)

一种好的方法是初始化一个变量,该变量存储您的最高然后在您的迭代块内生成一个随机数,如果它大于您的最大值,则将其设置为新的最大值。

Random r = new Random();
int max = 0; //declare our max variable
for(int i = 0; i < 100; i++)
{
    int rand = r.Next(0, 1001);
    if(rand > max) //if the new random value is greater than our max, set max = rand
        max = rand;
}

Console.WriteLine(max); //Output the maximum value
Console.ReadLine(); 

如果要输出每个随机值,然后输出所有生成的值中的最大值,只需在循环中也输出rand即可修改上面的代码。

希望这会有所帮助!

答案 4 :(得分:0)

如果您想查看所有循环/数组/随机代码的协同工作,则可以将以下内容与注释一起使用,逐行说明(Working .NET Fiddle Example

public static void Main()
{
    // Pass in what range we want our randomly generated numbers to be in
    // In your case, between 1 - 1000 and we want to create 100 of them.
    //(See GenerateRandomNumbers())

    var random = GenerateRandomNumbers(1, 1000, 100);

    //Take our newly returned randomly created numbers and 
    //pass them to our GetMaxNumber method so it can find the Max number
    //See (GetMaxNumber())

    var result = GetMaxNumber(random);

    //We now have our max number; print it to the Console.

    Console.WriteLine("Max: " + result);
}

public static int GetMaxNumber(params int[] inputs)
{
    //Create a variable that will store the largest number we find in our array

    int max = inputs[0];

    //Iterate (loop) through all of the 100 values in our array that we passed in
    //Here we define "input" which will hold the value for each value in inputs as we check
    //if the value of input is greater than our current value of max. If it is greater than our
    //current value of max, then we need to update max to now be equal to the value of our input.
    //Note: it will do this comparison 100 times beginning with the first value in the inputs array

    foreach (var input in inputs)
    {
        if (input > max)
        {
            //input's value is greater than the current value of max; update max so that it is equal to the current value of input.

            max = input;
        }
        //no more code; return to top of foreach loop and set input to the next value in inputs
    }

    //When we get here, it means our foreach loop has completed going through and comparing all 100 values of inputs to see which value is the largest.
    //now return this value to Main()

    return max;
}

public static int[] GenerateRandomNumbers(int beginRange, int endRange, int maxNumbers)
{
    // Instantiate random number generator

    Random rnd = new Random();

    //Generate and display 

    int[] intArr = new int[maxNumbers];

    //Generate 100 numbers with numbers between 1 and 1000

    for (int i = 0; i < intArr.Length; i++)
    {
        int num = rnd.Next(beginRange, endRange);
        intArr[i] = num;
    }

    return intArr;
}