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

Task: Fix Random.NextInt64() and Random.NextSingle() extension methods #4

Open
NightOwl888 opened this issue Feb 19, 2024 · 0 comments
Assignees

Comments

@NightOwl888
Copy link
Owner

NightOwl888 commented Feb 19, 2024

When the underlying random class is J2N.Randomizer, we need to cascade the calls to NextBoolean(), NextInt64() and NextSingle() to the underlying implementation.

Since there will eventually be another subclass of System.Random in this library in future versions of Lucene, we should do this by creating an interface in J2N so we can tell whether these methods are implemented (since they aren't prior to .NET 6.0). This will allow us to test for the presence of that interface and call the method only if it exists, falling back to our current implementation.

.NET 8 also introduces the Shuffle method on the Random class. In J2N, it exists in ListExtensions. We should add the implementation to the Randomizer class and also fix the existing Shuffle method to cascade the call to Randomizer after checking for its interface.

Proposed Interfaces

namespace J2N
{
    public interface IRandomWithInt64
    {
        long NextInt64();
    }
    public interface IRandomWithSingle
    {
        float NextSingle();
    }
    public interface IRandomWithBoolean
    {
        bool NextBoolean();
    }
    public interface IRandomWithShuffle
    {
        void Shuffle<T>(Span<T> values); // (when supported)
        void Shuffle<T>(T[] values);
        void Shuffle<T>(IList<T> values);
    }
}

The NextInt64() method overloads can also be optimized by using an array pool, since the byte array is only a temporary buffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant