# Randomize value in array with Game Maker

In my game Another Door, you encounter a random monster each run. For that, I need to pick them from the array where every possible encounter belongs. In this tutorial, you’ll see 4 different methods to get a random value from an array with Game Maker:

- The easiest
- Marble Bag
- Weigthed random
- Choose function

## Easiest method to pick a random value from an array

Imagine you have an array filled with different values, like those 3 monsters: “Ratten Konig”, “Forgotten Slime”, and “Mommy long legs” and you want to pick one randomly, here is how:

```
// Set the array with our values
arrayMonster = ["Ratten Konig", "Forgotten Slime", "Mommy long legs"];
// Get length of the array
arrayLength = array_length(arrayMonster);
// Generate number
randomMonster = irandom(arrayLength-1);
// Pick a value
var _monsterPicked = arrayMonster[randomMonster];
```

**As a result, the variable _monsterPicked will be one of the 3 values from our array.**

To get a random number to pick in our array, we are using the function irandom() which returns a value between 0 and length-1 (because array index starts at 0 while array length counts start at 1) as an integer.

## Marble Bag random with Game Maker

Marble bag random, as explained in this video, is a bit different. Once you’ve chosen a value from the array, you want to exclude this value from the poll until every value has been picked once.

If we take back our code, we need to add two things:

- array_delete() – to delete the value
- A way to recreate the array once it is empty

```
arrayMonster = ["Ratten Konig", "Forgotten Slime", "Mommy long legs"];
arrayLength = array_length(arrayMonster),
randomMonster = irandom(arrayLength-1);
var _monsterPicked = arrayMonster[randomMonster];
// Delete the value picked
array_delete(arrayMonster, randomMonster, 1);
// If length == 1, we have picked the last value
// We can now refill our array with the default value
if(arrayLength==1) {
arrayMonster = ["Ratten Konig", "Forgotten Slime", "Mommy long legs"];
}
```

This way, we will pick a random value AND every value will be picked once.

As you can see, the line where we fill the array is duplicated. A good practice would be to create a function to do it instead of rewriting these lines again and again.

## Weigthed random by yal.cc

Imagine you want to have 3x more “Forgotten Slime” than other values in your array, that’s possible. You’ll need to use a type of random called “weighted random”. Personally, I’m using an algorithm made by Yal.cc which is working well.

I won’t reproduce the code here, everything is well explained on his own website; you can definitely have a look.

## The choose function

Lastly, I’ll mention another way of doing it, with the function `choose()`

. This function does not get values directly from an array but from the arguments you’ll pass. It’s very easy and fast to use if you want to prototype before adding complexity to your game, here is how:

```
// Set our var with the choose() function
monster = choose("Ratten Konig", "Forgotten Slime", "Mommy long legs");
```

This will set our var `monster`

to “Ratten Konig”, “Forgotten Slime” or “Mommy long legs” randomly, with 33.333% chance each.

Thanks for reading, see you.