Say I have an array like so:
const alphabet = ['a', 'b', 'c', 'd'];
This represents 4 political candidates and a rank choice vote, where candidate a
is first choice, b
is second choice, etc.
I want to shuffle this into a bunch of random orders, but in this case I want a
to appear first with probably 60%, b
second with probability 20%, and c
third with probability 10%, and all the other ordering with probably 10%. Is there some lodash and ramda functionality that can accomplish this or?
This is for testing a rank choice voting algorithm. Shuffling the array randomly yields candidates that all have pretty much identical vote counts which doesn't mirror most reality (although I will test for that too).
I have this pretty horrible routine which will generate one random array:
const getValues = function () {
const results = [];
const remaining = new Set(alphabet);
const probabilities = [0.6, 0.2, 0.1, 0.1];
for(let i = 0; i < alphabet.length; i++){
const r = Math.random();
const letter = alphabet[i];
if(r < probabilities[i] && remaining.has(letter)){
results.push(letter);
remaining.delete(letter);
}
else{
const rand = Math.floor(Math.random()*remaining.size);
const x = Array.from(remaining)[rand];
remaining.delete(x);
results.push(x);
}
}
return results;
};
this "works" but doesn't quite order things according to the specified probabilities, because of conditional probability. Does someone know of a good way to have the order appear with certain probability, as I described above?
Here is some sample output that I am looking for:
[ [ 'd', 'b', 'a', 'c' ],
[ 'a', 'b', 'c', 'd' ],
[ 'a', 'd', 'b', 'c' ],
[ 'd', 'b', 'a', 'c' ],
[ 'b', 'c', 'a', 'd' ],
[ 'a', 'b', 'c', 'd' ],
[ 'd', 'b', 'c', 'a' ],
[ 'c', 'd', 'a', 'b' ],
[ 'd', 'b', 'a', 'c' ],
[ 'a', 'b', 'c', 'd' ] ]
if you generated enough data it wouldn't fit the desired order/distribution.