Weighted `chance.pick`?
See original GitHub issueI’ve been using chance for a couple of weeks now and I’m finding that I’m having code like the following:
var statuses = [ 200, 200, 200, 200, 200, 200, 304, 404, 404, 403, 403, 500 ];
console.log(chance.pick(statuses));
I’m doing this because in the above case I’m wanting to simulate more 200’s than anything else and more 404/3’s than the remming options. This is a “poor” attempt at creating “weighted” pick.
I’m wondering… If there is any desire to better support weighted chance.pick
and if so what it would look like?
Issue Analytics
- State:
- Created 9 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Random Pick with Weight - LeetCode
Random Pick with Weight - You are given a 0-indexed array of positive integers w where w[i] describes the weight of the ith...
Read more >What is the weighted random selection algorithm? - Educative.io
We need to implement an algorithm that selects indices randomly with respect to their weights. svg viewer. Perform weighted random selection of indices....
Read more >weighted - Chance.js
Provide an array of items, and another array of items specifying the relative weights and Chance will select one of those items, obeying...
Read more >Python Weighted Random Choices from the list with Probability
Weighted random choices in Python. Choose elements from the list randomly with a different probability. Generate weighted random numbers.
Read more >How to get weighted random choice in Python? - GeeksforGeeks
Weighted random choices mean selecting random elements from a list or an array by the probability of that element.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is created and available in the latest version of Chance on npm, 0.6.0
Totally understood, and I didn’t mean to imply that developers should be forced to do messy math themselves — just offering my example of how I’ve done it in the past. That said, I don’t feel it totally rules out that method behind the scenes; perhaps Chance could take the user’s given weight set and scale it so that it sums to 1.