Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random generation of enums never includes the last enum-value #79

Closed
PureKrome opened this issue Feb 22, 2018 · 3 comments
Closed

Random generation of enums never includes the last enum-value #79

PureKrome opened this issue Feb 22, 2018 · 3 comments
Assignees
Labels
Bug
Milestone

Comments

@PureKrome
Copy link
Contributor

@PureKrome PureKrome commented Feb 22, 2018

So if I wish to generate a random enum using this code ...

var statusType = GetRandom.Enumeration<StatusType>();

the result will be random, but it will NEVER include the last enum value.

Update

My initial reason was wrong, which was why I crossed out the convo below but didn't delete it for future reference, etc.


why? cause of this..

image

So basically, with the .NET Random.Next(min, max) method :-
- min is INCLUSIVE (i.e. a possible random result)
- max is EXCLUSIVE (i.e. will never be a possible random result)

So to me this is crazy talk, but I cannot change the way Random.Next is implemented.


So this means, to fix this issue ... I'm not going to make any suggestions about all the non-enum methods, but for the enum one, I can just bump the max value.

PR incoming...

@PureKrome

This comment has been minimized.

Copy link
Contributor Author

@PureKrome PureKrome commented Feb 22, 2018

Ok - so it seems like the code looks ok, but it still errors. So, heres a repo which can reproduce the errors:

using FizzWare.NBuilder;
using FizzWare.NBuilder.Generators;
using System;
using Xunit;

namespace nbuild_tests
{
    public class UnitTest1
    {
        private readonly Random rnd = new Random(System.Guid.NewGuid().GetHashCode());

        [Fact]
        public void TestPasses()
        {
            int counter = 0;
         
            for(int i = 0; i < 100000; i++)
            {
                var values = EnumHelper.GetValues(typeof(StatusType));
                var index = Next(0, values.Length);
                var statusType = (StatusType)values.GetValue(index);

                if (statusType == StatusType.Pink_LastItemInEnumCollection)
                {
                    counter++;
                }
            }

            Assert.True(counter > 0);
        }

        [Fact]
        public void TestFails_ButShouldPass()
        {
            int counter = 0;
         
            for(int i = 0; i < 100000; i++)
            {
                var statusType = GetRandom.Enumeration<StatusType>();
                if (statusType == StatusType.Pink_LastItemInEnumCollection)
                {
                    counter++;
                }
            }

            Assert.True(counter > 0);
        }

        // internal "Next" commad for getting the next random number.
        public virtual int Next(int min, int max)
        {
            return rnd.Next(min, max);
        }

        // Test Enum.
        public enum StatusType
        {
            Unknown,
            Red,
            Green,
            Blue,
            Pink_LastItemInEnumCollection
        }
    }
}
@crmckenzie crmckenzie self-assigned this Jul 6, 2018
@crmckenzie crmckenzie added the Bug label Jul 6, 2018
crmckenzie added a commit that referenced this issue Jul 6, 2018
@crmckenzie crmckenzie added this to the 6.0.0 milestone Jul 6, 2018
@crmckenzie

This comment has been minimized.

Copy link
Collaborator

@crmckenzie crmckenzie commented Jul 6, 2018

Fixed in develop.

@crmckenzie crmckenzie closed this Jul 6, 2018
Doug-Murphy added a commit to Doug-Murphy/nbuilder that referenced this issue Nov 20, 2019
* Bump Tests project from .NET Core 1.1 to 2.1 since 1.1 is no longer supported.
@Doug-Murphy

This comment has been minimized.

Copy link
Contributor

@Doug-Murphy Doug-Murphy commented Nov 20, 2019

This is still an issue for the other Enumeration(Type type) method. PR incoming.

crmckenzie added a commit that referenced this issue Dec 17, 2019
* Bump Tests project from .NET Core 1.1 to 2.1 since 1.1 is no longer supported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.