question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weighted `chance.pick`?

See original GitHub issue

I’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:closed
  • Created 9 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
victorquinncommented, Sep 2, 2014

This is created and available in the latest version of Chance on npm, 0.6.0

0reactions
davmillarcommented, Aug 26, 2014

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.

On Aug 25, 2014, at 9:30 PM, Victor Quinn notifications@github.com wrote:

@davmillar I considered this, but my whole concept with Chance was to get developers out of the business of doing complicated math to generate randomness.

By doing fractional decimal math like that, it requires developers to ensure everything adds to 1 in order for things to work which gets messy. I like the idea of doing weights because they can be arbitrarily sized to fit the constraints and for ease of readability.

In my example above, it was convenient to use percentages so I used factors of 10:

chance.weighted([chance.natural, chance.word, chance.ip], [60, 30, 10])() // or a simpler example chance.weighted([‘dog’, ‘cat’, ‘fish’], [60, 30, 10]) which yields ‘dog’ 60% of the time, ‘cat’ 30% of the time, and ‘fish’ 10% of the time. But the same exact result could be achieved in my proposed solution by using single digits:

chance.weighted([‘dog’, ‘cat’, ‘fish’], [6, 3, 1]) or even fractions:

chance.weighted([‘dog’, ‘cat’, ‘fish’], [0.6, 0.3, 0.1]) and if I accidentally do something silly like this:

chance.weighted([‘dog’, ‘cat’, ‘fish’], [0.6, 0.3, 0.2]) everything will still work and nothing will blow up, it’ll just be weighted appropriately. (54.54% dog, 27.27% cat, 18.18% fish)

And then if I want to add another option with say, a very small probability, it’s as easy as:

chance.weighted([‘dog’, ‘cat’, ‘fish’, ‘who knows’], [6000, 3000, 1000, 1]) if I’m doing that for a test, I don’t have to think about or care that ‘who knows’ has a 1/10001 probability, I probably don’t care, I likely just want it to happen infrequently.

So this is why I thought that using a weight of arbitrary size and having Chance figure out the proportionality for you made sense and was the cleanest, easiest way to do this.

In a world where everything has to add up to 1 I’d end up with something like:

chance.weighted([‘dog’, ‘cat’, ‘fish’, ‘who knows’], [0.6, 0.3, 0.999, 0.001]) and that’s a bit of a brain bender, if one decimal is off by one digit it would be an error. I’d prefer to avoid those.

— Reply to this email directly or view it on GitHub.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found