Skip to content

Commit

Permalink
[KED-1647] Refactor random data generator, and add layers (#161)
Browse files Browse the repository at this point in the history
The random data generator now assigns layers in a way that make the ranks towards the top have more nodes than the ones at the bottom, which is more in line with how charts should behave in reality.

I've also added layers, and randomly assigned the number of ranks per layer, so that they will grow and shrink each time.
  • Loading branch information
richardwestenra authored May 11, 2020
1 parent 58151b5 commit 43b3d8d
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 101 deletions.
7 changes: 3 additions & 4 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ const getDataSource = () => {
let source;
const qs = hasWindow && window.location.search.match(/data=(\w+)/);
const { REACT_APP_DATA_SOURCE } = process.env;
const isDemo =
hasWindow && window.location.host === 'quantumblacklabs.github.io';

if (qs) {
source = encodeURIComponent(qs[1]);
} else if (REACT_APP_DATA_SOURCE) {
source = REACT_APP_DATA_SOURCE;
} else if (
hasWindow &&
window.location.host === 'quantumblacklabs.github.io'
) {
} else if (isDemo) {
source = 'demo';
}
return validateDataSource(source);
Expand Down
28 changes: 28 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export const randomIndex = n => Math.floor(Math.random() * n);
*/
export const randomNumber = n => Math.ceil(Math.random() * n);

/**
* Get a random number between min and max, inclusive
* @param {number} (min) Min number
* @param {number} (max) Max number
*/
export const randomNumberBetween = (min, max) => randomNumber(max - min) + min;

/**
* Get a random datum from an array
* @param {Array} range The array to select a random item from
Expand All @@ -61,6 +68,27 @@ export const getRandomName = (n, join = '_') =>
.map(() => getRandom(LOREM_IPSUM))
.join(join);

/**
* Randomly select a certain number (n) of items from an array (arr).
* via https://stackoverflow.com/a/19270021/1651713
* @param {array} arr List from which to choose
* @param {number} n Number of items to select
*/
export const getRandomSelection = (arr, n) => {
const result = new Array(n);
let len = arr.length;
const taken = new Array(len);
if (n > len) {
return arr;
}
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
}
return result;
};

/**
* Filter duplicate values from an array
* @param {any} d Datum
Expand Down
Loading

0 comments on commit 43b3d8d

Please sign in to comment.