A sound can be represented mathematically by a sine wave with a certain amplitude, frequency, and phase. We can randomly select frequencies from a list specified in http://en.wikipedia.org/wiki/Piano_key_frequencies that comply with the following formula:
n is the number of the piano key. We will number the keys from 1 to 88. We will select the amplitude, duration, and phase at random.
Begin by initializing random values, then generate sine waves, compose a melody, and finally, plot the generated audio data with matplotlib: