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

JavaScript MillerShuffle can output negative numbers #7

Open
kadoban opened this issue Jan 2, 2025 · 0 comments
Open

JavaScript MillerShuffle can output negative numbers #7

kadoban opened this issue Jan 2, 2025 · 0 comments

Comments

@kadoban
Copy link

kadoban commented Jan 2, 2025

MillerShuffle(3, 612489217, 10) is -5 , which I would not expect. I thought the output should be between 0 and 9, inclusive.

I believe this happens because the math was copied over from C, but the C code is using unsigned int and JavaScript ^ on "number" uses essentially signed 32 bit, if I'm understanding correctly.

So the calculation for r2 for those input values ends up negative, which doesn't look intended.

r2=((randR*0x89)^r1)%p2;

The naive fix might be to do that ^ in BigInt and then truncate with BigInt.asUintN(32,...) and get back to number-land that way? Eg:

r2=Number(BigInt.asUintN(32, BigInt(randR*0x89)^BigInt(r1)))%p2;

I'm not 100% sure if that breaks the logic or if there's other places that can similarly overflow. Safest might be doing all of the math in BigInt, truncating with asUintN(32,...) at many of the steps and then converting to number at the end, but I bet many of them are unnecessary, it's just not obvious to me which need it and which don't.

The only one that obviously does need it to me is that part of the calculation of r2 that I showed above.

kadoban added a commit to kadoban/Miller_Shuffle_Algo that referenced this issue Jan 2, 2025
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