-
Notifications
You must be signed in to change notification settings - Fork 715
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
new(mock-data, demo): add stable randomness #1033
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9b47280
deps(mock-data): d3-random@^2.2.2 for seeded random
williaster 68b0fed
new(mock-data): add getSeededRandom, support seeds in all generators
williaster 81a41ea
test(mock-data): add tests for seeded random across generators
williaster 92b40fa
new(mock-data): export getRandomNormal
williaster d8ae983
internal(demo): use stable randomness for all demos
williaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 12 additions & 5 deletions
17
packages/visx-demo/src/sandboxes/visx-drag-i/generateCircles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
import { getSeededRandom } from '@visx/mock-data'; | ||
|
||
export interface Circle { | ||
id: string; | ||
radius: number; | ||
x: number; | ||
y: number; | ||
} | ||
|
||
const generateCircles = ({ width, height }: { width: number; height: number }) => | ||
new Array(width < 360 ? 40 : 185).fill(1).map((d, i) => { | ||
const radius = 25 - Math.random() * 20; | ||
const generateCircles = ({ width, height }: { width: number; height: number }) => { | ||
const radiusRandom = getSeededRandom(0.2); | ||
const xRandom = getSeededRandom(0.3); | ||
const yRandom = getSeededRandom(0.4); | ||
|
||
return new Array(width < 360 ? 40 : 185).fill(1).map((d, i) => { | ||
const radius = 25 - radiusRandom() * 20; | ||
return { | ||
id: `${i}`, | ||
radius, | ||
x: Math.round(Math.random() * (width - radius * 2) + radius), | ||
y: Math.round(Math.random() * (height - radius * 2) + radius), | ||
x: Math.round(xRandom() * (width - radius * 2) + radius), | ||
y: Math.round(yRandom() * (height - radius * 2) + radius), | ||
}; | ||
}); | ||
}; | ||
|
||
export default generateCircles; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
packages/visx-demo/src/sandboxes/visx-streamgraph/generateData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
import getSeededRandom from './getSeededRandom'; | ||
|
||
export interface DateValue { | ||
date: Date; | ||
value: number; | ||
} | ||
|
||
export default function genDateValue(length: number): DateValue[] { | ||
export default function genDateValue( | ||
length: number, | ||
/** Optional random seed in the interval [0, 1). */ | ||
seed?: number, | ||
/** Optional start time in ms UTC. */ | ||
startTimeMs?: number, | ||
): DateValue[] { | ||
const random = seed == null ? Math.random : getSeededRandom(seed); | ||
const startDateMs = startTimeMs == null ? Date.now() : new Date(startTimeMs).valueOf(); | ||
return new Array(length).fill(1).map((_, idx: number) => { | ||
return { | ||
date: new Date(Date.now() - idx * 3600000), | ||
date: new Date(startDateMs - idx * 3600000), | ||
// eslint-disable-next-line no-bitwise | ||
value: Math.max(250, (Math.random() * 3000) | 0), | ||
value: (random() * 3000) | 0, | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { randomLcg } from 'd3-random'; | ||
|
||
// returns a seeded random number generator | ||
export default function getSeededRandom( | ||
/** Seed in the interval [0, 1). */ | ||
seed: number, | ||
) { | ||
return randomLcg(seed); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,45 @@ | ||
import { genBin } from '../src'; | ||
import { genBin, getSeededRandom } from '../src'; | ||
|
||
describe('generators/genBin', () => { | ||
test('it should be defined', () => { | ||
it('should be defined', () => { | ||
expect(genBin).toBeDefined(); | ||
}); | ||
|
||
test('it should have shape [{ bin, count }]', () => { | ||
it('should have shape [{ bin, count }]', () => { | ||
const bin = genBin(1); | ||
expect(bin).toHaveLength(1); | ||
expect(bin[0].bin).toBeDefined(); | ||
expect(bin[0].count).toBeDefined(); | ||
}); | ||
|
||
test('it should take optional bin function', () => { | ||
it('should take optional bin function', () => { | ||
const bin = genBin(1, i => i); | ||
expect(bin[0].bin).toEqual(0); | ||
}); | ||
|
||
test('it should take an optional count function', () => { | ||
it('should take an optional count function', () => { | ||
const bin = genBin(1, undefined, i => i); | ||
expect(bin[0].count).toEqual(0); | ||
expect(bin[0].bin).toEqual(0); | ||
}); | ||
|
||
it('should support seeded randomness', () => { | ||
const n = 3; | ||
const seededRandom1 = getSeededRandom(0.5); | ||
const seededRandom2 = getSeededRandom(0.5); | ||
|
||
expect( | ||
genBin( | ||
n, | ||
i => i, | ||
(i, number) => 25 * (number - i) * seededRandom1(), | ||
), | ||
).toEqual( | ||
genBin( | ||
n, | ||
i => i, | ||
(i, number) => 25 * (number - i) * seededRandom2(), | ||
), | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
support for
seeds
was just added 🎉