0

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

  • 2
    `static main()` is the entry point to a console application. If you don't have that, your application cannot start. Make your `main()` static. –  Apr 25 '17 at 23:58
  • _"It should also...check to ensure it doesnt include the same randomly generated number twice."_ -- just FYI: if you do that, then your numbers aren't actually random. They are just a plain sequence of numbers, shuffled. – Peter Duniho Apr 26 '17 at 00:04
  • If you are running in the debugger, it should show you what exception is being thrown and terminating the program. Based on the code you show above, I'm going to guess `StackOverflowException`, because your `Contains()` check seems to be backwards. Should be `if (generated.Contains(number)) { ... }`, rather than `if (!generated.Contains(number)) { ... }`. Not that that's a good way of picking unique numbers, but at least it would work better. – Peter Duniho Apr 26 '17 at 00:06
  • @PeterDuniho nice catch. and `generatenewnumber()` will need to be static as well, since `Main()` is static –  Apr 26 '17 at 00:08
  • That you got far enough for the program to run and close, suggests that the code you posted above is not the code in actual use (because, as noted in the comment above, the entry point must be a `static` method). So you're left with how to pick the unique random numbers. Your basic bug is a typo. See marked duplicates for additional information on how to implement this. – Peter Duniho Apr 26 '17 at 00:10

0 Answers0