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

Clean up Get-Random cmdlet #9133

Merged
merged 7 commits into from
Mar 16, 2019
Merged

Conversation

pougetat
Copy link

PR Summary

This PR cleans up the coding style in the class implementing Get-Random to make it consistent with the rest of the codebase

PR Context

This PR is linked to the following one and should be merged beforehand : #9111.
Once it is merged I will checkout 9111 and rebase master.

PR Checklist


#endregion

#region Cmdlet processing methods

private double GetRandomDouble(double min, double max)
{
double randomNumber;
double result;
Copy link
Collaborator

Choose a reason for hiding this comment

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

The original name makes more sense. Please revert.

}

return randomNumber;
Debug.Assert(min <= result, "lower bound <= random number");
Debug.Assert(result < max, "random number < upper bound");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why first condition is <= but second one <?

Copy link
Author

Choose a reason for hiding this comment

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

GetRandomDouble returns a random number between minValue (included) and maxValue (excluded). Hence the presence of the do{}while(randomNumber >= maxValue) loop

randomUint64 = BitConverter.ToUInt64(buffer, 0);
// Get the last 'bitsToRepresentDiff' number of randon bits
randomUint64 &= mask;
} while (uint64Diff <= randomUint64);

double result = min * 1.0 + randomUint64 * 1.0;

Debug.Assert(min <= result, "lower bound <= random number");
Debug.Assert(result < max, "random number < upper bound");
Copy link
Collaborator

Choose a reason for hiding this comment

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

The same.

Copy link
Author

Choose a reason for hiding this comment

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

For this case I'm not too sure.
At line 347 it will try to enforce maxValue as an excluding upper bound due to the Next(min, max) method but in this scenario I'm not sure if it will enforce maxValue as an exclusive upper bound.
In doubt I think it's best to at least stick with previous behavior in which the Assert came after the function call thus trying to enforce maxValue as an excluded upper bound in all cases. It's safer to have a script fail than to have it run with an unexpected value.
@PaulHigin ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd expect the same behavior (included or excluded) for both low and high boundaries.
Although we can guarantee this only for integers.

Copy link
Author

@pougetat pougetat Mar 14, 2019

Choose a reason for hiding this comment

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

In that case :

  • do you consider this resolved ?
  • do you want different groups of debug.assert statements for each of the two scenarios I listed where each assert correctly follows what the generator should return as a value ?
  • do you want a single debug.assert group which would be this one but with randomValue <= upper bound ?
  • do you want @PaulHigin 's take on this ?

😄

Copy link
Collaborator

Choose a reason for hiding this comment

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

@pougetat Please remove all controversial changes from this PR so that we can move forward

Copy link
Author

Choose a reason for hiding this comment

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

ok ill put the debug.assert statements back where they were.

/// <returns></returns>
public int Next(int minValue, int maxValue)
public int Next(int min, int max)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The original names makes more sense. Please revert.

Copy link
Contributor

@RDIL RDIL left a comment

Choose a reason for hiding this comment

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

Remove extra spaces

RDIL and others added 4 commits March 14, 2019 17:31
…m.Tests.ps1

Co-Authored-By: pougetat <thomas.pouget-abadie@ensimag.grenoble-inp.fr>
…m.Tests.ps1

Co-Authored-By: pougetat <thomas.pouget-abadie@ensimag.grenoble-inp.fr>
Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

LGTM with one comment.

@iSazonov iSazonov self-assigned this Mar 15, 2019
@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Mar 15, 2019
…RandomCommand.cs

Co-Authored-By: pougetat <thomas.pouget-abadie@ensimag.grenoble-inp.fr>
@pougetat
Copy link
Author

@iSazonov Made the last modif :)
Thanks for the review

@iSazonov iSazonov merged commit 5277c79 into PowerShell:master Mar 16, 2019
@pougetat pougetat deleted the clean-getrandom branch March 16, 2019 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants