# Weighted “choose” function A little post about a little GameMaker script to randomly return one of the values while minding their "weights" - in other words, a "biased" version of choose.

## The idea

First, we add up chances from each option.

Then we generate a random number between 0 (incl.) and that number (excl.).

Then, we starting subtracting chances from this number until it reaches 0 or lower, at which point we return the associated item.

If you had a lot of options, it would make sense to perform a binary search instead.

## The code

For GameMaker Studio 2.3+:

```function choose_weighted() {
var n = 0;
for (var i = 1; i < argument_count; i += 2) {
if (argument[i] <= 0) continue;
n += argument[i];
}

n = random(n);
for (var i = 1; i < argument_count; i += 2) {
if (argument[i] <= 0) continue;
n -= argument[i];
if (n < 0) return argument[i - 1];
}

return argument;
}
```

For older versions:

```var n = 0;
for (var i = 1; i < argument_count; i += 2) {
if (argument[i] <= 0) continue;
n += argument[i];
}

n = random(n);
for (var i = 1; i < argument_count; i += 2) {
if (argument[i] <= 0) continue;
n -= argument[i];
if (n < 0) return argument[i - 1];
}

return argument;
```

If you are sure that you'll never pass in negative chances, you can remove the if (argument[i] <= 0) continue; lines.

The last return statement will never execute (as random(n) cannot exceed n), but it is good practice to return on all paths in your functions.

And that's all!

### Related posts:

This site uses Akismet to reduce spam. Learn how your comment data is processed.