关于RNG和静态无效主体的混淆

时间:2017-04-25 23:55:36

标签: c# random

在我们班上,我们有一个创建随机数生成器的任务。 假设发生器在高范围和低范围之间产生X个随机生成的数字。它还应该将生成的项放入一个数组中并检查以确保它不包括两次相同的随机生成的数字。

Visual Studio没有出现任何错误,但可能会有一些人看到,当我尝试测试时,程序会立即关闭。

之前的错误消息告诉我该程序没有静态主要使用,但现在只是关闭而没有任何反馈。

有关为何发生这种情况的任何想法? (可能是明显的东西,我对c#很新)

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Randomnumbers
{
    class Program
    {
        int lowrange = 0; // Establishing the variables for lowest number, highest number, and total numbers generated.
        int highrange = 50;
        int numberof = 10;

        public void Main(string[] args)
        {
            while (numberof > 0) //Repeats this loop once for each "numberof". (until the number hits zero)
            {
                numberof = numberof - 1;
                generatenewnumber(); //Calls the function above
            }
            if (numberof == 0)
            {
                Console.ReadLine(); // Here so the program doesnt instantly close
            }
        }

        public void generatenewnumber()
        {
            var rng = new Random();
            int number = rng.Next(lowrange, highrange); //Sets the number to a random number between the high and low values.
            var generated = new List<int>(); // Creates a list of numbers that have been generated already to prevent double-ups.
            if (!generated.Contains(number)) // Checks if the rg'd number has already been made.
            {
                generatenewnumber(); //Calls the function above
            }
            else
            {
                numberof = numberof - 1;
                generated.Add(number); // Adds the genereated number to the list.
                Console.WriteLine(number); // Prints the number. 
                Console.ReadLine();
            }
        }
    }
}

谢谢:p

0 个答案:

没有答案