W.A. Mozarts Musical Dice

This page implements a musical game that was published in Mozarts name in 1787.

The edition of the work this page is based on (PDF download), ask the participant to throw two dice 16 times, look up the results in a table, copy one of 16×11=176 measures from the score and then play the resulting piece. This method allows 1116 different pieces to be generated, a total of 45 949 729 863 572 161, more than 45 Million Billion.

So Mozarts claim „to compose without the least knowledge of music so much German Walzer or Schleifer as one pleases“ certainly is no exaggeration.

To play the game as Mozart intended first select the radio-button „Roll dice“ at the top. Then click „Roll!“. The program rolls the dice and creates a signature. Last click on „Compose!“ and the system generates the corresponding piece of music in various formats for download and playback.

You can note the signature and come back and regenerate your personal piece from it at any time.

You can also use an arbitrary string like your name or date of birth to create a signature and have Mozart compose an individual piece for you. It is fantastically impropable that your piece has ever been played before or any other name to generate the same piece.


How does it work?

This application is intentionally built more complex than necessary. It is a toy project and combines arbitrary languages and technologies that I wanted to play with, resulting in a lot of moving parts.

Architectural overview


With a single exception the webserver is tasked with delivering static content. Something really every webserver can do. The exception being mozart.php which is described below.


The browser is used to call up index.html and loads various resources like images, stylesheets and javascript-code. The page uses jQuery and Bootstrap for styling and „enliving“ the page.

The main piece of code in mozart.js is a single asynchronous AJAX-call to mozart.php. That server-side script is responsible for starting the „Composition“ and returns a key identifying the request. The key points to a folder on the server. From that key and well-known filenames the client-side Javascript constructs HTML to display and downloading of results.


mozart.php Is a trivial piece of glue-code. Called as a CGI script by the webserver ot delegates to mozart.py. Input is the text entered by the user or the signature passed in as query-parameters. These parameters are passed to mozart.py weiter. When the composition is finished the request-ID is returned to the browser as plain text. You can call mozart.php directly, for instance as mozart.php?arg=Sample. The text that is returned can then be used to manually construct download-URLs for the results.


This is a python script that takes text and creates a Mozart-Signature from it. As a first step a MD5-hash over the test is calculated. The 16 bytes making up the hash are a good match for the 16 throws specified by Mozart. A byte, however, has 256 different values while throwing two dice can only yield 2..12, so 11 different values. mozart.py distributes the 256 byte-values to 11 by a simple modulo operation. This is slightly biased and underrepresents 10, 11 and 12. Given the fact that Mozart did not care about the non-uniform distribution of the 11 values when throwing dice I think that is acceptable.

mozart.py also generates a UID to be used a the request-ID.

That ID and the Mozart-Signature calculated is now passed to initialize.ck. Finally it calls lilypond to do typesetting of the score and to generate the MIDI file.


initialize.ck launches a set of ChuCK-Programs that finally implement the rules set by Mozart. These programs in a mildly object-oriented fashion model the measures of the original work, make the selection based on the signature and tables, and „play“ them using two virtual hands on virtual instruments.

Output from this stage is a WAV-file with the music and as side-effect one file containing lilypond-Notation for the left and right hand respectively.


lilypond is the last step in the processing and typesets the score as PDF and PNG. It also writes a MIDI-file that is more compact than the WAV-version and also sounds better with most synthesizers.