ExtensionMethod.NET Home of 858 C#, Visual Basic, F# and Javascript extension methods

Maximum number in Array of Random Numbers

Make an array of generated random numbers and get the maximum number every time you run.

Source

private static Random random = new Random();
public static int MaxArrayRandomNumber(this int[] array)
{
    int maxnumber = array[0];
    if (array is null) { return  0; }
    else
    {
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = random.Next();
        }

        for (int i = 0; i < array.Length; i++)
        {
            maxnumber = Math.Max(maxnumber, array[i]);
        }
    }
    return maxnumber;
    
}

Example

int[] arr = new int [5];
Console.WriteLine(arr.MaxArrayRandomNumber());

Author: Mustafa Elkholy

Submitted on: 9 jan 2023

Language: csharp

Type: system.Int32

Views: 92