Home Reference Source

scripts/experiment-properties/distribution/random_distribution_generator.js

export {generateRandomDistribution};
/**
 * Generates a random population, that can exclude a coordinate set if desired.
 *
 * @param  row                   {int} The number of possible unique x coordinates the points can land on
 *         col                   {int} Same as above but for y coordinates
 *         num_points            {int} The number of target points to be plotted
 *         excluding_coordinates {array of coordinates to exclude}
 *                               Set this to null if do not want to exclude anything
 *
 * @return coordinates          [ [x1, y1], [x2, y2] ... ]
 */
function generateRandomDistribution(row, col, num_points, excluding_coordinates) {

    let coordinates = [];
    while (coordinates.length < num_points) {
        let coord = generate_random_coordinate(row, col);

        if (!coordinates.includes(coord)){
            if (excluding_coordinates === null){
                coordinates.push(coord);
            } else if (!excluding_coordinates.includes(coord)) {
                coordinates.push(coord);
            }
        }
    }
    return coordinates;
}

/**
 * Generates random coordinate given row and column values.
 *
 * @param  row                  {int}
 *         col                  {int}
 *
 * @return [row, col] (JSON stringified so can do object comparisons)
 */
function generate_random_coordinate(row, col) {
    let x = get_random_int(row);
    let y = get_random_int(col);

    return [x,y];
}

/**
 * Generates random integer given max value.
 *
 * @param  max                   {int}
 * @return integer
 */
function get_random_int(max) {
    return Math.floor(Math.random() * Math.floor(max));
}

/**
* Converts the coordinates into this format for d3:
* [ [x1, y1], [x2, y2] ... [xn, yn] ]
* And samples the distribution for the specified num_points.
*
* @param coordinates { {x_values: [], y_values: []} }
*        num_points {integer}
* @return output_coordinates { [x1, y1], [x2, y2] ... }
*/
function prepare_coordinates(coordinates, num_points){

    var array = [];
    var reorganized_coordinates = [];
  
    for (let i = 0; i < coordinates.x_values.length; i++){
      array.push(coordinates.x_values[i]);
      array.push(coordinates.y_values[i]);
  
      reorganized_coordinates.push(array);
      array = [];
    }
    return reorganized_coordinates;
  }

/**
* Randomly picks x number of points from the distribution
* where x = num_points
* Method is currently unused but kept in case of use for further extension
* @param coordinates { [x1, y1], [x2, y2] ... }
*        num_points {integer}
* @return output_coordinates with size num_points { [x1, y1], [x2, y2] ... }
*/
function sample_coordinates(coordinates, num_points){
    var output_coordinates = [];
  
    for (let i = num_points; i > 0; i-- ) {
      var random_coordinate = coordinates.splice(Math.floor(Math.random() * (i + 1)), 1)[0];
      output_coordinates.push(random_coordinate);
    }
  
    return output_coordinates;
  }