Beta

Random Value in a Weighted List

Description:

Write a function that, given a list of weighted values, will return a random value using the weights to determine the probability of each value being returned.

Example:

const values  = ['a','b','c','d','e'];
const weights = [ 1,  2,  3,  4,  5 ];

const randomValue = weightedRandom(values, weights);

In this example the probability of each value being selected would be as follows:

  • a : 0.066 ( 1/15 )
  • b : 0.133 ( 2/15 )
  • c : 0.2 ( 3/15 )
  • d : 0.266 ( 4/15 )
  • e : 0.333 ( 5/15 )
Algorithms

Stats:

CreatedApr 10, 2017
PublishedApr 11, 2017
Warriors Trained146
Total Skips25
Total Code Submissions504
Total Times Completed38
JavaScript Completions38
Total Stars6
% of votes with a positive feedback rating71% of 21
Total "Very Satisfied" Votes11
Total "Somewhat Satisfied" Votes8
Total "Not Satisfied" Votes2
Total Rank Assessments20
Average Assessed Rank
6 kyu
Highest Assessed Rank
1 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • halcarleton Avatar
  • Voile Avatar
Ad