vendredi 21 avril 2017

RNG unknown error

Trying to make code that randomly generates X amount of random numbers between a high and a low value. The code also should prevent identical random numbers from being generated.

Here is the code i have so far:

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

namespace Randonnumbers
{
    class Program
    {

        static void Main(string[] args)
        {
            int lowrange = 0; // Establishing the variables for lowest number, highest number, and total numbers generated.
            int highrange = 50;
            int numberof = 10;
            var generated = new List<int>(); // Creates a list of numbers that have been generated already to prevent double-ups.
            var rng = new Random();
            Console.WriteLine("Numbers Generated:");
            while (numberof > 0) //Repeats this loop once for each "numberof". (until the number hits zero)
            {
                generatenewnumber(); //Calls the function above
            }
            Console.ReadLine(); // Here so the program doesnt instantly close
        }

        public void generatenewnumber()
        {
            int number = rng.Next(lowrange, highrange); //Sets the number to a random number between the high and low values.
            if (!generated.Contains("number")) // Checks if the rg'd number has already been made.
            {
                generatenewnumber(); //If it has, it will re-run this function.
            }
            else
            {
                numberof = numberof - 1;
                generated.Add(number); // Adds the genereated number to the list.
                Console.WriteLine(number); // Prints the number. 
            }
        }
    }
}

Not sure where the error is. The program will not run and does not display any errors.

Perhaps it has to do with calling the function from inside itself on a condition? Honestly have no idea, i am pretty new to c#.c#




Aucun commentaire:

Enregistrer un commentaire