MATLAB Dice Simulator
2024

This project showcases object-oriented programming, test suites, and app design for MATLAB through defining and building virtual dice.

Source code hosted on GitHub

Object-Oriented Design

The abstract base class (ABC) Die is used to define the properties and methods all dice should share. There are two types of dice implemented from Die: NumericDie and CategoricDie. NumericDie can be used to create a typical numbered die, while CategoricDie uses strings for the sides of the die. Supporting functions for the project which don't belong in a specific class are kept in the +probability package.

The class structure is mirrored by the test classes, with a separate test class for the +probability package. The test classes include setup and teardown functions to ensure the test suite executes at the correct directory level. This could be avoided by putting the tests in the same directory as the rest of the project, but it's best to avoid clutter. In other languages, import support is usually robust enough to avoid needing to change the working directory during execution.

A UML diagram of the project contents

Real Dice vs. Virtual Dice

Virtual dice can't compete with the satisfaction of rolling real dice, but they are certainly far more convenient. Virtual dice always land flat, can be rerolled in fractions of a second, and never roll off the table! Since the purpose of a die is to be random, virtual dice have no practical disadvantage compared to physical dice and also offer the advantage of fine control of the die's weighting. A virtual die could, for example, easilly be programmed with a Gaussian weighting to favor central values over extreme values.

A coin, d4, d6, d8, d10, d12, d20, and pair of d10s as a d100.

GM Roller App

But who really wants a virtual die if rolling real dice is so much more fun? An easy answer is game masters for tabletop gaming. Rolling dice starts to lose its appeal when you need to do it dozens or hundreds of times over the course of a few hours for a bunch of unique dice mixtures. The GM Roller app is designed to make it easier to manage all those dice rolls while also keeping a log of all the results!

Software Development in MATLAB

It is possible to perform full software development in MATLAB from source code to distribution. However, this is only a good idea for small projects. MATLAB's object-oriented design and testing support is limited compared to other languages, and the proprietary nature of the software can make it difficult to distribute even if you have the money to spend on licenses. The GM Roller app, for example, can only be opened by someone who also owns a MATLAB license. There is a MATLAB compiler which can build programs into standalone executables which can be run by the freely available MATLAB Runtime, but the compiler is not available with a home license.

Future Work

There are some other features which would be fun to explore:

There are also probably some bugs (I know of two so far in GMRoller). See if you can find them!