| 1 |
<CsoundSynthesizer> |
| 2 |
<CsOptions> |
| 3 |
-nm0 |
| 4 |
</CsOptions> |
| 5 |
<CsInstruments> |
| 6 |
ksmps = 32 |
| 7 |
seed 0 |
| 8 |
|
| 9 |
opcode ArrPermRnd, i[], i[] |
| 10 |
iInArr[] xin |
| 11 |
iLen = lenarray(iInArr) |
| 12 |
;create output array and set index |
| 13 |
iOutArr[] init iLen |
| 14 |
iWriteIndx = 0 |
| 15 |
iReadLen = iLen |
| 16 |
;for all elements: |
| 17 |
while iWriteIndx < iLen do |
| 18 |
;get one random element and put it in iOutArr |
| 19 |
iRndIndx = int(random:i(0, iReadLen-.0001)) |
| 20 |
iOutArr[iWriteIndx] = iInArr[iRndIndx] |
| 21 |
;shift the elements after this one to the left |
| 22 |
while iRndIndx < iReadLen-1 do |
| 23 |
iInArr[iRndIndx] = iInArr[iRndIndx+1] |
| 24 |
iRndIndx += 1 |
| 25 |
od |
| 26 |
;decrease length to read in and increase write index |
| 27 |
iReadLen -= 1 |
| 28 |
iWriteIndx += 1 |
| 29 |
od |
| 30 |
xout iOutArr |
| 31 |
endop |
| 32 |
|
| 33 |
;create i-array as 1, 2, 3, ... 12 |
| 34 |
giArr[] genarray 1, 12 |
| 35 |
|
| 36 |
;permutation of giArr ... |
| 37 |
instr Permut |
| 38 |
iPermut[] ArrPermRnd giArr |
| 39 |
printarray iPermut, "%d" |
| 40 |
endin |
| 41 |
|
| 42 |
;... which has not been touched by these operations |
| 43 |
instr Print |
| 44 |
printarray giArr, "%d" |
| 45 |
endin |
| 46 |
|
| 47 |
</CsInstruments> |
| 48 |
<CsScore> |
| 49 |
i "Permut" 0 .01 |
| 50 |
i "Permut" + . |
| 51 |
i "Permut" + . |
| 52 |
i "Permut" + . |
| 53 |
i "Permut" + . |
| 54 |
i "Print" .05 .01 |
| 55 |
</CsScore> |
| 56 |
</CsoundSynthesizer> |
| 57 |
;example by joachim heintz |