Headshota - 1 year ago 67

Javascript Question

I'm a bit confused about how to generate integer values with probabilities. to be clear as an example,

I have four integers with their probability values: 1|0.4, 2|0.3, 3|0.2, 4|0.1

how can I generate these four numbers taking into account their probabilities?

javascript implementation would be appreciated.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Here's a useful trick :-)

```
function randomWithProbability() {
var notRandomNumbers = [1, 1, 1, 1, 2, 2, 2, 3, 3, 4];
var idx = Math.floor(Math.random() * notRandomNumbers.length);
return notRandomNumbers[idx];
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**