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

refactor: replace pseudo-random number generator #1479

Merged
merged 2 commits into from
Aug 13, 2019
Merged

refactor: replace pseudo-random number generator #1479

merged 2 commits into from
Aug 13, 2019

Conversation

louh
Copy link
Member

@louh louh commented Aug 8, 2019

I've been doing a lot of learning on how seeding pseudo-random number generation works, and looked into how we implement it here, which has been largely untouched (besides some language refactors) since 2013. In particular, I wanted to know why the random seeds are occasionally modified with magic numbers (and the short answer is: no good reason, at least not anymore, so we can remove those parts of the code).

Furthermore I found the seedrandom package which seems to be really well made as a PRNG utility, so in this PR I propose we start using it. There is one more dependency as a result, but the alternative is to vendor it into the code directly, and in any case I don't think we need our own implementation of it (however simple). The other tradeoff from migrating to this package is that it does change the result of existing random seeds. It only affects the layout of people on sidewalks right now, and if there is anyone out there that was relying on specific seeds for a specific mix of people they're in for a bad time. But I think that'll be a really rare case and in people can always re-roll pedestrians.

@codecov-io
Copy link

codecov-io commented Aug 8, 2019

Codecov Report

Merging #1479 into master will increase coverage by 0.04%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1479      +/-   ##
==========================================
+ Coverage   33.91%   33.96%   +0.04%     
==========================================
  Files         248      248              
  Lines        9182     9169      -13     
  Branches     1986     1985       -1     
==========================================
  Hits         3114     3114              
+ Misses       5464     5451      -13     
  Partials      604      604
Impacted Files Coverage Δ
assets/scripts/segments/SegmentForPalette.jsx 96.15% <ø> (ø) ⬆️
assets/scripts/segments/buildings.js 21.34% <0%> (+0.23%) ⬆️
assets/scripts/palette/Palette.jsx 0% <0%> (ø) ⬆️
assets/scripts/segments/people.js 0% <0%> (ø) ⬆️
assets/scripts/util/random.js 33.33% <0%> (+26.66%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9840688...72c2db2. Read the comment docs.

getVariantInfoDimensions.mockImplementation(() => dimensions)
})
describe('on mount', () => {
it('connects to drag', () => {
shallow(<SegmentForPalette connectDropTarget={connectDropTarget} connectDragSource={connectDragSource} connectDragPreview={connectDragPreview} type={''} variantString={''} intl={intl} />)
shallow(<SegmentForPalette connectDropTarget={connectDropTarget} connectDragSource={connectDragSource} connectDragPreview={connectDragPreview} type={''} variantString={''} intl={intl} randSeed={42} />)
Copy link
Collaborator

Choose a reason for hiding this comment

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

"The Answer to the Ultimate Question of Life, The Universe, and Everything."


for (let i = 1; i < floors; i++) {
const variant = (building.variantsCount === 0) ? 0 : Math.floor(randomGenerator.rand() * building.variantsCount) + 1
const variant = (building.variantsCount === 0) ? 0 : Math.floor(randomGenerator() * building.variantsCount) + 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

🤔 part of me wonders if we should explore using UUIDs, but that's definitely out of scope here

Copy link
Collaborator

@whitmanschorn whitmanschorn left a comment

Choose a reason for hiding this comment

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

I love it! Especially the documentation. 🚢

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

Successfully merging this pull request may close these issues.

None yet

3 participants