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

PredefinedRandom fails when generating integers due to overflow #26

Open
mitkonikov opened this issue Dec 1, 2024 · 0 comments
Open

Comments

@mitkonikov
Copy link

mitkonikov commented Dec 1, 2024

PredefinedRandom's nextInt() function is overflowing. Running this simple test:

RNG.setSelectedRandomGenerator(RngType.PREDEFINED_RANDOM);
for (int i = 0; i < 20; i++) {
    System.out.println(RNG.nextInt());
}

One can see that it will generate -1 many times and only negative numbers. This is indicative of overflow.

The nextInt() function should be fixed as follows:

@Override
public int nextInt() {
    double normalized = (getNextPredefinedNumber() % 1.0);
    return (int)(Integer.MIN_VALUE + (long)(normalized * ((long)Integer.MAX_VALUE - Integer.MIN_VALUE)));
}

The (normalized * range) should be kept long instead of int due to its size.

I will push a pull request for a new algorithm soon and push this change as well. Tell me if you want a separate PR for this change.

PS: I'm also currently working on improving the Point class and a few other things.

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