You’re on the right track but possibly missing a few things.
First, a major scale has semi-tone and tone intervals. Adding a random value of an int between 0 - 7 will create only 7 semi-tones, including sharps and flats.
To stay in C major you need to add specific intervals to midi note 60. For ex. adding 2 semi-tones to cpsmidinn(60) would give you D (midi note 62), adding 5 would be an F note. The easiest way to do that is likely with an ftable (or array). Another way to do this would be to store the midi note numbers in an ftable.
It can be tricky to schedule these type of events within a single instr. as when the instr is called in the score and also called with a schedule things like envelopes and variables can get confused - for example the initial score event will hold the envelope for p3 and also for each schedule event.
It may be easier to instead use one instr to trigger (schedule) another. Here is an example. One trig instr adds the values of ftable2, the second trig instr uses midi notes directly instead of adding them to p5 (60).
Also, in your schedule, p5 is not modulating filter cutoff as p5 is never applied to the filter.
If you have any questions feel free to ask.
Scott
<CsoundSynthesizer>
<CsOptions>
;-odac ; Disable real-time audio output
-o output.wav ; Output to a WAV file
</CsOptions>
<CsInstruments>
sr = 44100 ; Sample rate
ksmps = 32 ; Control rate
nchnls = 2 ; Number of output channels
0dbfs = 1
instr trigger
kTrig metro 1 ; trigger instr 1 once per second
kRand = int(trandom(kTrig, 0, 8)) ; create random number every time metro creates a trigger
kPitch table kRand, 1 ; randomly choose index from ftable 1 to create a new note
schedkwhen kTrig, 0, 0, 1, 0, .4, p4, p5 + kPitch
endin
instr trigger2
kTrig metro 1 ; trigger instr 1 once per second
kRand = int(trandom(kTrig, 0, 8)) ; create random pitch
kPitch table kRand, 2
schedkwhen kTrig, 0, 0, 1, 0, .4, p4, kPitch
endin
instr 1
; Create the main sound using a resonant lowpass filter
aEnv madsr 0.2, 0.2, 0.4, 0.6
aSound vco2 p4, cpsmidinn(p5)
aSound butterlp aSound, 800
aSound = aSound * aEnv
; Apply panning
iPan random 0, 1
aLeft = aSound * (1 - iPan)
aRight = aSound * iPan
; Output the sound
outs aLeft, aRight
endin
</CsInstruments>
<CsScore>
; Define the function table
f1 0 8 -2 0 2 4 5 7 9 11 12 ; notes to add to midinn 60
f2 0 8 -2 60 62 64 65 67 69 71 72 ; midi notes (one octave of C major)
; Play the piece
i "trigger" 0 16 0.2 60 ; Instrument 1, start time 0, duration 180s, amplitude 0.2, frequency 60Hz
i "trigger2" 17 16 0.2
</CsScore>
</CsoundSynthesizer>