Melanie W
Melanie W

Reputation: 43

C#: Generate 100 random numbers between 1-1000 and output the max value

I'm very new to coding and I just can't wrap my head around Loops/Arrays/Randoms. I understand the concept but when it comes to applying it, I'm just lost.

Here I'm trying to generate 100 random numbers between 1-1000 and it has to output the maximum value. Here's my code so far:

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

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

It's only giving me one number. :( I'd greatly appreciate any help!

Thanks!

Upvotes: 2

Views: 15064

Answers (5)

Stemado
Stemado

Reputation: 629

If you want to see the code for Loops/Arrays/Randoms all working together you can use the below with the comments walking through what each line is doing (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;
}

Upvotes: 0

J. Hsu
J. Hsu

Reputation: 67

I am not sure, are you asking like this?

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);

Upvotes: 0

Rohan
Rohan

Reputation: 359

A good approach would be initializing a variable that stores your max. Then generate a random number within your iterative block and if it is greater than your max, set it as the new max.

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(); 

If you want to output every random value and then output the max out of all the values generated, simply modify the code above by outputting rand within your loop as well.

Hope this helps!

Upvotes: 0

er-sho
er-sho

Reputation: 9771

You can accumulate your random generated number to the array and then by using Max function of the array you can find the maximum value

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

Upvotes: 3

bielu000
bielu000

Reputation: 2241

You need to call rnd.Next() inside loop.

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

Console.ReadLine(); 

Upvotes: 0

Related Questions