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

[NO QA] Fix random numbers again #10700

Merged
merged 3 commits into from
Aug 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,28 +524,7 @@ function navigateToDetailsPage(report) {
* @returns {Number}
*/
function generateReportID() {
// Generate a random 32-bit number. This works fine, and starts us building a random 53-bit number.
let result = Math.floor(Math.random() * (2 ** 32));

// Now generate another random number. We'll use this for the remaining 21 bits of randomness.
let extraBits = Math.floor(Math.random() * (2 ** 32));

// Add each of the remaining 21 bits to the end of `result`.
for (let i = 0; i < 21; i++) {
// Shift left by one, but don't use bit shifts: they truncate to 32-bits.
result *= 2;

// If extraBits is odd, meaning the lowest bit is set, do the same to the result, without bitwise operators.
if (extraBits % 2 === 1) {
result += 1;
}

// Now drop the lowest bit from extraBits, which we can do with bitwise operators, because it's less than 32-bits.
// eslint-disable-next-line no-bitwise
extraBits >>= 1;
}

return result;
return (Math.floor(Math.random() * (2 ** 21)) * (2 ** 32)) + Math.floor(Math.random() * (2 ** 32));
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to see this moved over to NumberUtils.js instead of ReportUtils.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could do that, but it really shouldn't be used for anything but report ID's, everything else will be 64-bits. I don't have a hugely strong opinion, but leaving it here at least implies not to use it for other IDs.

Copy link
Contributor

Choose a reason for hiding this comment

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

We'll be using it for IOUReportIDs too, since those are integers as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that leaving it in reportUtils is fine since it's only used for reportIDs

}

/**
Expand Down