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

Fix Random.Next returns negative numbers #405 #840

Merged
merged 1 commit into from
Sep 5, 2018

Conversation

MatthiasJentsch
Copy link
Contributor

Description

We need to clear the first bit because rand->Next returns uint32 and the method needs to return int32.

Motivation and Context

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Signed-off-by: Matthias Jentsch info@matthias-jentsch.de

We need to clear the first bit because rand->Next returns uint32 and the method needs to return int32.
@nfbot
Copy link
Member

nfbot commented Sep 5, 2018

Hi @MatthiasJentsch,

I'm nanoFramework bot.
Thank you for your contribution!

A human will be reviewing it shortly. 😉

@@ -14,7 +14,7 @@ HRESULT Library_corlib_native_System_Random::Next___I4( CLR_RT_StackFrame& stack

NANOCLR_CHECK_HRESULT(GetRandom( stack, rand ));

stack.SetResult_I4( rand->Next() );
stack.SetResult_I4( rand->Next() & 0x7FFFFFFF );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't simply casting a less "expensive" operation as it just gets compiled without requiring any extra operations (ANDing with 0x7FFFFFFF)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Casting for instance 0xFFFFFFFF which is a valid return value from rand->Next() into an int32 results in the value -1. So I think we need ANDing or modulo operation like % 0x7FFFFFFF

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! so we have a winner! 👍

Copy link
Contributor

@doingnz doingnz Sep 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I am inclined to avoid "magic values" in code. Maybe in the future we could use & Int32.MaxValue which is equivalent to the implemented & 0x7FFFFFFF as it helps the source code to be "self documenting" as to why the specific mask is being applied.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doingnz I don't know if we have access to Int32.MaxValue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @doingnz .
There is a constant in the CRT lib with that value...
Or maybe a comment explaining why that is being done is good enough.

Copy link
Member

@josesimoes josesimoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@josesimoes josesimoes added Type: bug Area: Common libs Everything related with common libraries labels Sep 5, 2018
@MatthiasJentsch MatthiasJentsch merged commit b2e11da into develop Sep 5, 2018
@MatthiasJentsch MatthiasJentsch deleted the MatthiasJentsch-patch-1 branch September 5, 2018 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Common libs Everything related with common libraries Type: bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Random.Next returns negative numbers
4 participants