How to generate a random weighted distribution of elements

metalaureate

I would like to return an array which has a set of unique elements randomly distributed according to custom frequency. My real world use-case is the repetition of carousel images based on a qualitative weighting of how popular those images are.

E.g. suppose I have 5 elements with weights:

A, 20% B, 50% C, 80% D, 10%

I would like to write a function that, given a length, tries to approximate a sequence such that C will appear eight times more often than D; D will appear 5 times less often than B; A will appear three times less often than C.

Drakes

C will appear eight times more often than D; D will appear 5 times less often than B; A will appear three times less often than C.

You can do that with a weighted array of your elements:

var elems = ["A", "B", "C", "D"];
var weights = [2, 5, 8, 1]; // weight of each element above
var totalWeight = weights.reduce(add, 0); // get total weight (in this case, 16)

function add(a, b) { return a + b; } // helper function

var weighedElems = [];
var currentElem = 0;
while (currentElem < elems.length) {
  for (i = 0; i < weights[currentElem]; i++)
    weighedElems[weighedElems.length] = elems[currentElem];
  currentElem++;
}

console.log(weighedElems);

This will produce an array like

["A", "A", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "D"]

so you can choose randomly from that like so

var rnd = Math.floor(Math.random() * totalWeight);
console.log(weighedElems[rnd]);

Resources:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Generate A Weighted Random Number

How to generate a random normal distribution of integers

How to generate random numbers with predefined probability distribution?

How to generate a random probability distribution julia

Generate random numbers with weighted probabilites

How to make a random function in fortran to generate the same random distribution into array?

selecting weighted random distribution from a mysql table

How to generate n random numbers from negative binomial distribution?

How do I generate discrete random events with a Poisson distribution?

How to generate random numbers with a normal distribution based on endpoints in excel?

How to generate a random normal distribution with specific standard deviation

How to generate random categorical data in python according to a probability distribution?

How to generate random numbers with skewed normal distribution in R?

Generate random numbers with a given distribution

Generate random bytearray from a distribution

Generate random number from an arbitrary weighted list

How to use numpy.random to generate random numbers from a certain distribution?

How to generate n random numbers from a normal distribution using random-fu (Haskell)?

Generate random numbers with logarithmic distribution and custom slope

Generate random numbers with a given (numerical) distribution

Generate random number belonging to exponential distribution

Generate random number from custom distribution

Generate random numbers from lognormal distribution in python

Generate random numbers with bivariate gamma distribution in R

Generate a random number from normal distribution in J

Generate a random distribution by group conditional on a column

Generate random variables from a probability distribution

Generate random numbers with a numerical distribution in given intervals

Generate random numbers from a normal distribution