Repository navigation
[No QA] Upgrade random number generators with more significant bits. - #10592
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@roryabraham @luacmartins @stitesExpensify @srikarparsi
Details
Per the discussion linked in the issue, most JS implementations seem to implement
Math.random()using 32-bit numbers.What does this mean in practice? The JS engine generates a 32-bit random number and "stretches" it to fit the space that
Math.random()returns (which is[0, 1))Here's a simplified version of a way this could work. It's not exactly the way it actually works, but it illustrates the problem. Pretend instead of 32-bit random numbers, JS generated 8-bit random numbers. They have a range of
[0-255].It then "scales these numbers" to fit in the space
[0, 1)by doing something like:Now, the max value for this to generate is 255, which will return
0.99609375which is between 0 and 1.The lowest value it can generate is 0, which will return
0.0.If you run this function a few times, you will see that it always generates a random number between 0 and 1 (excluding 1 itself) and it looks "correct".
You can then multiply the result by some big number to get a number between
0andNumber.MAX_SAFE_INTEGERand what it looks like is that you've generated a random number between0and9007199254740991, which seems like it has a very large range of possible values.BUT, there are only 256 possible values that are ever generated. Yes, they're all between
0and9007199254740991but they are not every value between those numbers. In fact, there are only 256 possible values that are ever returned.You can see that if we did such a thing, we'd start having collisions with IDs very quickly. With 32-bit numbers, we have ~4.2 billion possible values, so there are fewer collisions, but the goal of large random numbers is to be able to generate every possible integer in the 53 or 64 bit range, which we currently are not doing.
This change makes us never use the result of Math.random() for more than 32-bits of precision, and instead generates 2 (or 3, for 64-bit numbers) random numbers and combines them together, to give us the precision we actually expect to have.
Fixed Issues
$ https://github.com/Expensify/Expensify/issues/224792
Tests
I tested the update functions in the browser (via copy/paste) and verified they outputted values in the ranges they're intended to. Note that this does not actually check that there are more significant bits than before, which is not actually very possible, but it does verify they still produce random numbers in the correct range.
PR Review Checklist
Contributor (PR Author) Checklist
### Fixed Issuessection aboveTestssectionQA stepssectiontoggleReportand notonIconClick)src/languages/*filesSTYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)/** comment above it */displayNamepropertythisproperly so there are no scoping issues (i.e. foronClick={this.submit}the methodthis.submitshould be bound tothisin the constructor)thisare necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);ifthis.submitis never passed to a component event handler likeonClick)StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)Avataris modified, I verified thatAvataris working as expected in all cases)PR Reviewer Checklist
The Contributor+ will copy/paste it into a new comment and complete it after the author checklist is completed
### Fixed Issuessection aboveTestssectionQA stepssectiontoggleReportand notonIconClick).src/languages/*filesSTYLE.md) were followedAvatar, I verified the components usingAvatarhave been tested & I retested again)/** comment above it */displayNamepropertythisproperly so there are no scoping issues (i.e. foronClick={this.submit}the methodthis.submitshould be bound tothisin the constructor)thisare necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);ifthis.submitis never passed to a component event handler likeonClick)StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)Avataris modified, I verified thatAvataris working as expected in all cases)QA Steps
Screenshots
Web
Mobile Web
Desktop
iOS
Android