Results 1 to 9 of 9

Thread: Random.Next() never returning the number 0

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2018
    Location
    Sofia, Bulgaria
    Posts
    5
    Rep Power
    0

    Random.Next() never returning the number 0

    Hello everyone,

    I'm kind of new to C# in general (only made a few console apps around two years ago) and am making a Random Number Generator app for Windows. Everything works perfectly except one thing:
    Whenever I enter 0 as the lower bound and tick the checkbox to include it (so the result can contain a 0 as well) the whole program crashes for some reason. I tested the function in a seperate console app by doing:
    Code:
    Random random = new Random();
    Console.WriteLine(random.Next(0, 2));
    The above code should print either 0 or 1 and indeed it does print both numbers (one at a time and randomly ofcourse) so why does my Forms app not like it when I include 0?
    I'm attaching my app here. Any help is appreciated
    Attached Files Attached Files
    Last edited by JohnH; 11-03-2018 at 7:43 AM. Reason: removed generated binaries from attachment (bin + obj folders)

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,954
    Rep Power
    151
    Please don't attach a zipped solution except as a last resort. The first option should always be to post the relevant code and only the relevant code directly.

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2018
    Location
    Sofia, Bulgaria
    Posts
    5
    Rep Power
    0
    Quote Originally Posted by jmcilhinney View Post
    Please don't attach a zipped solution except as a last resort. The first option should always be to post the relevant code and only the relevant code directly.
    OK, sorry. Thought it would be easier to start my app and see where the problem could be.

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,954
    Rep Power
    151
    It would be easier to look at the relevant code in this thread and see where the problem is than downloading, extracting and opening your project. If we can't work it out from there then we can always ask for the whole project. It is sometimes required but rarely, thus it should not be the first option. The first option should be the relevant code, so...

  5. #5
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2018
    Location
    Sofia, Bulgaria
    Posts
    5
    Rep Power
    0
    So here's the function that generates the numbers:

    private void generateRandomNumbers(object sender, EventArgs e)
    {
    if ((this.numbersAmountInputBox.Text != null) && (this.rangeFromInputBox.Text != null) && (this.rangeToInputBox.Text != null))
    {
    bool amountNumsIsOK = Int32.TryParse(this.numbersAmountInputBox.Text, out int amountNums);
    bool fromNumIsOK = Int32.TryParse(this.rangeFromInputBox.Text, out int fromNum);
    bool toNumIsOK = Int32.TryParse(this.rangeToInputBox.Text, out int toNum);


    this.resultBox.Clear();


    if ((amountNumsIsOK) && (fromNumIsOK) && (toNumIsOK))
    {
    amountNumsGlobal = amountNums;


    int[] array = new int[amountNums];
    Random random = new Random();
    int temp = 0;
    int min = 0;
    int max = 0;


    if ((this.includeFromCheckBox.Checked) && (this.includeToCheckBox.Checked))
    {
    min = fromNum;
    max = toNum + 1;
    }
    else if ((this.includeFromCheckBox.Checked) && (!this.includeToCheckBox.Checked))
    {
    min = fromNum;
    max = toNum;
    }
    else if ((!this.includeFromCheckBox.Checked) && (this.includeToCheckBox.Checked))
    {
    min = fromNum + 1;
    max = toNum + 1;
    }
    else if ((!this.includeFromCheckBox.Checked) && (!this.includeToCheckBox.Checked))
    {
    min = fromNum + 1;
    max = toNum;
    }


    if (min >= max)
    {
    this.resultBox.Clear();
    this.resultBox.Text += "Please make sure that the borders are set correctly: From < To!";
    }
    else if ((min < max) && (max - min < amountNums))
    {
    this.resultBox.Clear();
    this.resultBox.Text += "Please make sure that there are enough numbers inside the borders: To - From >= Amount!";
    }
    else if ((min < max) && (max - min >= amountNums))
    {
    if (this.resultTypeComboBox.SelectedIndex == 0)
    {
    for (int i = 0; i < array.Length; i++)
    {
    temp = random.Next(min, max);
    while (isDuplicate(temp, array))
    {
    temp = random.Next(min, max);
    }
    array[i] = temp;
    }
    }
    else if (this.resultTypeComboBox.SelectedIndex == 1)
    {
    for (int i = 0; i < array.Length; i++)
    {
    temp = random.Next(min, max);
    while (isDuplicate(temp, array))
    {
    temp = random.Next(min, max);
    }
    array[i] = temp;
    }
    Array.Sort(array);
    }
    else if (this.resultTypeComboBox.SelectedIndex == 2)
    {
    for (int i = 0; i < array.Length; i++)
    {
    temp = random.Next(min, max);
    while (isDuplicate(temp, array))
    {
    temp = random.Next(min, max);
    }
    array[i] = temp;
    }
    Array.Sort(array);
    Array.Reverse(array);
    }


    for (int i = 0; i < array.Length; i++)
    {
    this.resultBox.Text += array[i] + " ";
    }


    defaultArray = new int[amountNumsGlobal];
    string values = this.resultBox.Text;
    string[] tokens = values.Split(' ');


    for (int i = 0; i < tokens.Length - 1; i++)
    {
    defaultArray[i] = Int32.Parse(tokens[i]);
    }
    }
    }
    else
    {
    this.resultBox.Clear();
    this.resultBox.Text += "Please enter only whole positive integer numbers in all blank input spaces!";
    }


    generateButtonPressed = true;
    }
    }

    The checkboxes if-else statements are for including/excluding the lower/upper bounds in the result so I guess that's where my problem would be at.
    Whenever I enter 0 as the lower bound and tick the checkbox to include it (so the result can contain a 0 as well) and set the borders to exactly the amount of available numbers including zero the whole program crashes for some reason.

    Example:
    Amount of nums to generate: 2
    Lower bound inclusive: 0
    Upper bound inclusive: 1
    Result: Crash (Not Responding), while it should show me a result containing the numbers 0 and 1 in it (0 1 or 1 0, duplicates not allowed).

    If I make it like this:
    Amount of nums to generate: 2
    Lower bound inclusive: 0
    Upper bound inclusive: 2
    The result always contains the numbers 1 and 2 but NEVER the number 0, although it is included and should show up from time to time (as the generation is random, but when I spam the generate button it must show at least once).

    Weird thing is that when I do in a seperate console app the above test with the 0 and 1 inclusive it does show 0, so the problem isn't in the Random.Next() function. What's wrong?
    Last edited by MegasXLR; 11-04-2018 at 5:10 AM. Reason: Explained a bit better & sorry for not posting code before, now I know ^^

  6. #6
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,954
    Rep Power
    151
    Have you actually debugged that code, i.e. set a breakpoint and stepped through it line by line, examining the state at each step? If you're saying that it crashes under certain circumstances when it should do something else seems to suggest not. You should do that now. At the very least, you can explain to us exactly where the actual state differs from your expectations and what data is in use at the time, but you should be able to do more than that. If you don't know how to debug, start learning here:

    https://msdn.microsoft.com/en-us/lib...or=-2147217396

    As well as debugging, you need to learn how to write test code. I said earlier that you should post the RELEVANT code and there's far more than that there. Stack Overflow makes a recommendation that you post a Minimal, Complete & Verifiable Example and you should do that here and everywhere else too. What that means is to strip out everything that isn't directly relevant tot he issue. You should be doing that for yourself first though. That means putting your project aside and creating new test project with the simplest possible code to test the specific functionality you are having issues with. The simpler the code is, the easier it is to narrow down exactly where the issue is. If things work as expected, add one more feature. Continue in that vein until things break. You then know that the last change you made is at least partly responsible for the issue so you have inherently narrowed it down considerably.

    The way to not go about things is to write all your code, run it and then post the lot on public forums and wait for it to be fixed. There's a lot more to software development than writing and reading code. Debugging and testing is where you spend most of your time, if you consider design part of software engineering rather than development.

  7. #7
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2018
    Location
    Sofia, Bulgaria
    Posts
    5
    Rep Power
    0
    I used a MessageBox.Show() on each step where problem might have been and sure enough I found out what it was
    Sadly an empty array created with "int[] array = new int[number]" is filled with zeros in C#, not null. I think in C++ it wasn't like that, but could be wrong. And I have a check for duplicates in the array:
    Code:
    for (int i = 0; i < array.Length; i++)                            {
          temp = random.Next(min, max);
           while (isDuplicate(temp, array))
            {
                   temp = random.Next(min, max);
            }
            array[i] = temp;
    }
    So it turns out that when I enter and include 0 in the input it gets stuck in this loop forever as all values of the array are 0. Is there a way to make the array truly empty (aka not have any zeros in it after initialization)?

    Since my RNG works between 0 and 99 I can always just make all values of the array have value 100 after its initialization and then everything would work perfectly but I'd rather do it the right way if possible.
    Last edited by MegasXLR; 11-07-2018 at 5:03 AM.

  8. #8
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Location
    Norway
    Posts
    431
    Rep Power
    109
    Integer is a value type and can not be null, for that you need Nullable types (C# Programming Guide) | Microsoft Docs.
    Alternative to array is to use a list that you add items to until the amount you need.
    [xcode=c#] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

  9. #9
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2018
    Location
    Sofia, Bulgaria
    Posts
    5
    Rep Power
    0
    Quote Originally Posted by JohnH View Post
    Integer is a value type and can not be null, for that you need Nullable types (C# Programming Guide) | Microsoft Docs.
    Alternative to array is to use a list that you add items to until the amount you need.
    Thanks, will try it out.

Similar Threads

  1. Question number is the same as the random number?
    By ribbann in forum C# General Discussion
    Replies: 7
    Last Post: 09-06-2016, 11:32 AM
  2. Question How do I create a random number between two numbers with exceptions?
    By Fanis in forum VS.NET General Discussion
    Replies: 2
    Last Post: 09-06-2015, 11:53 AM
  3. Question Doing a Binary Search and Returning a lookup
    By ghaffar3 in forum C# General Discussion
    Replies: 0
    Last Post: 05-29-2014, 3:16 AM
  4. Question Returning multiple values from called methods
    By Gerald in forum C# General Discussion
    Replies: 2
    Last Post: 01-10-2013, 9:16 PM
  5. Replies: 4
    Last Post: 08-17-2012, 3:17 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •