VBA – Simulation of Rolling Dices

Excel and VBA are a powerful tool for simulation.

The idea is to simulate dice throwing and to keep the best N throws as our score. Other model variables are Times I roll, which stands for how many times do we roll the dice and Number of sides, which is self-explanatory. At the picture below, a standard 6-side dice is considered:

After running the code twice, we get this:

The colors in purple are the top results, which are taken to our score. In the lone below, they are colored in yellow. After the third run, this is how the result looks like:

The main part of the VBA is this:

It actually looks quite self-explanatory. We “read” the three parameters, color back everything in white and we generate the random number with makeRandom. Then, through the Transpose Trick we get an array, which is passed to the functions WriteTopN and ColorTopCells.

This is how these two subs look like:

At the end, these are the two standard functions for generating a random number and getting the last column:

Cheers and thank you for reading!

Tagged with: , , , ,