mardi 25 avril 2017

Confusion regarding RNG and static void main

In our class, we have a task to create a random number generator. The generator is supposed to make X number of randomly generated numbers between a high and a low range. It should also put generated items into an array and check to ensure it doesnt include the same randomly generated number twice.

Visual studio is not coming up with any errors, though there probably are some seeing as the program instantly closes when i attempt to test run it.

The previous error message told me that the program did not have a static main to use, though now its just closing without any feedback.

Any ideas on why this is happening? (could be something obvious, i am quite new to c#)

Code:

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

Thanks :p




Aucun commentaire:

Enregistrer un commentaire