0

Theres probably something wrong with the flow of logic in this code, though i thought i would post it here to see if its something simple i am missing. (It probably is, as i am currently quite new to c#).

This script is supposed to generate X amount of random numbers between a High value and a Low value, as well as ensuring it does not generate the same random number twice.

Right now, the script is not having any errors, but is only creating one random number, regardless of the defined 'amount' of numbers to create.

I tried changing "int numberof = 11; to a public int, but apparently that breaks the entire program and i dont know why.

Anyway, support is appreciated. Thanks :)

Code:

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

namespace Randomnumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            int numberof = 11;
            Console.WriteLine("Numbers Generated:");
            while (numberof > 0) //Repeats this loop once for each "numberof". (until the number hits zero)
            {
                numberof = numberof - 1;
                generatenewnumber(); //Calls the function
            }
            if (numberof == 0)
            {
                Console.ReadLine(); // Here so the program doesnt instantly close
            }
        }

        static void generatenewnumber()
        {
            var rng = new Random();
            int lowrange = 0;
            int highrange = 50;
            int numberof = 11;
            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.
            while (numberof > 0)
            {
                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();
                }
            }
        }
    }
}
  • You don't want to do this recursively, but the main problem is that you're creating a `new Random()` every iteration of a tight loop, meaning it'll be initialized with the same seed value (the current time) and produce the same sequence. – CodeCaster Apr 26 '17 at 23:07
  • You should camelCase your local variables and PascalCase your method names to make the code more readable. And your GenerateNewNumber method should probably take in an argument specifying how many numbers you want, and then return the list of numbers it generates. – Rufus L Apr 26 '17 at 23:10
  • @CodeCaster see my answer [here](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number/43646093#43646093) – CodingYoshi Apr 26 '17 at 23:18
  • @CodingYoshi I think that answer doesn't really answer the question you posted it to. – CodeCaster Apr 26 '17 at 23:21
  • @CodeCaster then why did you mark this as a duplicate of that? – CodingYoshi Apr 26 '17 at 23:27
  • Because _this_ question is _"Why does my number generator function generate the same number"_, which the duplicate addresses, and your answer answers the question _"How can I generate some numbers within a certain range"_. – CodeCaster Apr 26 '17 at 23:28
  • No it generates unique numbers within a certain range. Anyhow sorry for asking you to see my answer. I meant to tell the OP. – CodingYoshi Apr 26 '17 at 23:29
  • @CodingYoshi standard answer to "unique random numbers" is pick as necessary while shuffleing at the same time for reasonably sized ranges (O(1) for each number or O(numbers_to_pick) total)... And your code does not work anyway for huge ranges when you need to pick almost all numbers. – Alexei Levenkov Apr 26 '17 at 23:58
  • @alexeilevenkov can you elaborate please? Why wouldn't my answer work? I can see it will take longer as it fills up but why won't it work? Any reading you can suggest please – CodingYoshi Apr 27 '17 at 01:10
  • I've commented and voted accordingly on your post (in my first comment I missed the fact code is just incorrect) – Alexei Levenkov Apr 27 '17 at 04:24

0 Answers0