如何生成1到500之间的1000个随机数,并找到最小值,最大值和平均值? [英] How to generate 1000 random numbers between 1 and 500, and find minimum, maximum and average?

查看:114
本文介绍了如何生成1到500之间的1000个随机数,并找到最小值,最大值和平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;

public class Test
{
	public static void Main()
	{
		Random myRandom = new Random(1, 1001);
        int A = new int[1000];
        int i = 0;

        While (i < 1001) 
        
        A[i] = myRandom.Next(1, 501);
        i ++;
        }
        
		int output1 = Math.Min(i); 
		Console.WriteLine("The minimum number is: " + output1);
		
		int output2 = Math.Max(i);
		Console.WriteLine("The maximum number is: " + output2);
		
		int output3 = Average(params int[] A);
		Console.WriteLine("The average of these numbers is: " + output3);
    
        }
	public static int Math.Min(int i) //To calculate the minimum//
	{
    	        int answer = i;
      	        return answer;
	}
	
	public static int Math.Max(int i) //To calculate the maximum//
	{
		int answer = i;
		return answer;
	}
	
	public decimal Average(params int[] A) //To calculate the average//
        {
    	 	
                int sum = Sum(A);
                decimal result = (decimal)sum / A.Length;
                return result;
     
        }
	}

}

推荐答案

你可能的意思

You possibly meant
using System;

public class Test
{
    public static void Main()
    {
        const int NUMBERS = 1000;
        Random myRandom = new Random();
        int [] a = new int[NUMBERS];

        int i = 0;
        int max = int.MinValue, min = int.MaxValue, sum = 0;

        while (i < NUMBERS)
        {
          a[i] = myRandom.Next(1, 501);
          if (max < a[i]) max = a[i];
          if (min > a[i]) min = a[i];
          sum += a[i];
          ++i;
        }
        double avg = (double)sum / NUMBERS;


        Console.WriteLine("The minimum number is: {0}", min );
        Console.WriteLine("The maximum number is: {0}", max);
        Console.WriteLine("The average of these numbers is: {0}", avg);

  }
}







请注意你实际上不喜欢我需要将提取的数字存储在数组中,以便计算最小值,最大值和平均值。





[更新]




Please note you actually don't need to store the extracted numbers in the array in order to compute minimum, maximum and average.


[Update]

Quote:

感谢您的建议和解决方案,真的很有帮助。我想知道如何将命令用于查找不同方法中数字的最小值,最大值和平均值?

Thanks for the advice, and the solution, really helpful. I was wondering how I would go about putting the commands for finding the minimum, maximum and average of the numbers in different methods?

然后必要的修改是您需要将数组传递给相关方法,例如



Then the essential modification is you need to pass the array to the relevant method, e.g.

public static int find_min(int[] a)
{
  int min = int.MaxValue;

  for (int n = 0; n < a.Length; ++n)
  {
    if (min > a[n]) min = a[n];
  }
  return min;
}





[/ Update]



[/Update]


using System;

public class Test
{
    public static void Main()
    {
        const int NUMBERS = 1000;
        Random myRandom = new Random();
        int [] a = new int[NUMBERS];

        int i = 0;
        int max = int.MinValue, min = int.MaxValue, sum = 0;

        while (i < NUMBERS)
        {
          a[i] = myRandom.Next(1, 501);
          if (max < a[i]) max = a[i];
          if (min > a[i]) min = a[i];
          sum += a[i];
          ++i;
        }
        double avg = (double)sum / NUMBERS;


        Console.WriteLine("The minimum number is: {0}", min );
        Console.WriteLine("The maximum number is: {0}", max);
        Console.WriteLine("The average of these numbers is: {0}", avg);

  }
    public static int find_min(int[] a)
    {
        int min = int.MaxValue;
        for (int n = 0; n < a.Length; ++n)
    {
        if (min > a[n]) min = a[n];
    }
        return min;
    }

    public static int find_max(int[] a)
    {
        int max = int.MinValue;
        for (int n = 0; n < a.Length; ++n)
        {
        if (max < a[n]) max = a[n];
        }
        return max;
    }

    public static double find_sum(int[] a)
    {
        sum = 0;
        double avg = (double)sum / NUMBERS;
        for (int n = 0; n < a.Length; ++n)
        {
            sum += a[n];
        }
        return avg;
    }
}


int average = 0;
int largestNumber = 0;
int smallestNumber = int.MaxValue;
int _avr = 0;
int index = 0;

while (index < 1000)
{
    byte[] ba = new byte[4];
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    rng.GetBytes(ba);
    int n2 = BitConverter.ToInt32(ba, 0);

    Random rd = new Random(n2);
    int n = rd.Next(1, 501);

    if (n > largestNumber)
        largestNumber = n;
    if (n < smallestNumber)
        smallestNumber = n;
    _avr = _avr + n;

    index++;
}

average = _avr / 1000;


这篇关于如何生成1到500之间的1000个随机数,并找到最小值,最大值和平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆