In this paper, byte-pair coding is used to automatically learn the optimal combination between notes and chords, so that there are stronger dependencies between notes and chords in the sliced music blocks. Based on byte-pair coding to encode the information of the main melodic part of the original music, and after matrix transformation of the sample songs to obtain the note transfer probability matrix, the Markov chain in the stochastic process model is utilized to design an automatic music generation model. After preprocessing, experimental simulation is conducted in light of the flaws in music creation, and the music generated using the stochastic process model is examined following the pertinent evaluation indices. The findings suggest that the music generation technique suggested in this thesis is effective, as the UPC index value stays consistent between 3 and 5 throughout the training phase. Using stochastic process model-based music generation makes it easier for students to acquire music knowledge. This study has successfully aided in the development of music instruction in colleges and universities and has generated a more significant function in supporting the teaching of music education in these settings.