0

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#

2 Answers2

0

In the line:

        if (!generated.Contains("number")) // Checks if the rg'd number has already been made.
        {
            generatenewnumber(); //If it has, it will re-run this function.
        }

Should !generated.Contains("number") not have a number ie

!generated.Contains(8);?

or a variable

int temp = 8;
!generated.Contains(temp);

Although very unlikely, calling "generatenewnumber()" within itself has the potential to infinitely generate numbers that are already within the list. What if you set up a do/while() loop: something like

    public void generatenewnumber()
    {
        int number;
        do
        {
            number = rng.Next(lowrange, highrange); //Sets the number to a random number between the high and low values.
        }while(generated.Contains(number)) // Checks if the rg'd number has already been made.

        numberof = numberof - 1;
        generated.Add(number); // Adds the genereated number to the list.
        Console.WriteLine(number); // Prints the number. 
    }

This removes the recursive calls which could be causing your problem

Lee Epp
  • 61
  • 5
  • oops, i left the "" in on that line... (fixed to remove the ""s since the 'number' is the variable for generated number.) However, the code still doesnt run... any other ideas :p – Quinnos Romynos Apr 22 '17 at 04:45
0

There are several problems here:

First: You are trying to use variables with a local scope in a different function.

The functiongeneratenewnumber() has never heard of lowrange, highrange, numberof or generated.

There are two ways of fixing that. One is to pass the needed variables as arguments of the function:

public static void generatenewnumber(int lowrange, int highrange, int numberof, List<int> generated) 

The other one is to declare them globally. For that you would have to declare them inside the class, but outside of any function.

Second: You attmpt to call a function and have it permanently change the values it is passed, like for example with numberof. This does not work this way. Normally parameters are "call by value", which means, that the variable in the function is just a copy of the original one, all changes are not persistent. If you want them to be persistent, you have to do a "call by reference". That might look like follows:

public static void generatenewnumber(int lowrange, int highrange, ref int numberof, ref List<int> generated)

And get called like this:

generatenewnumber(lowrange, highrange, ref numberof, ref generated);

Third: rng.Next is not a real command. You find the correct way to do it here: How do I generate a random int number in C#?

Community
  • 1
  • 1
Ruben Bohnet
  • 392
  • 1
  • 12