First time here? Check out the FAQ!

Random Numbers, but like 'urn' in Max/MSP~

0 votes

In Max, there is an object called 'urn'.

It generates a random number up to a set maximum, but does not repeat  until every number has been called, you can then reset it and start again.

Is there a way this can be achieved in Kyma? I found it very useful when experimenting with melodic rows.

Thanks, Charlie.


asked May 2, 2018 in Capytalk & Smalltalk by charlienorton (Adept) (2,650 points)

1 Answer

+4 votes

Assuming you want random integer notenumbers, you could use the following (for the middle C octave):

((!Trigger nextIndexReset: !Reset) of: (60 to: 72) asArray shuffle) nn

where you could replace 60 and 72 with whatever minimum and maximum values you like.

Or if you have a short list and want to keep shuffling the elements like a deck of cards to build up a sequence of shuffled sequences, you could use:

| pseq ranSeq seqLen |
pseq := #(60 62 64 66 68 70 72).
seqLen := 1024.
ranSeq := pseq shuffle.
(seqLen / pseq size) ceiling timesRepeat: [ranSeq := ranSeq, pseq shuffle].
(((1 bpm: !BPM) nextIndexMod: ranSeq size) of: ranSeq) nn

Another expression that is not exactly what you want in this case but may come in handy sometimes is:

!Trigger nextRandomElementOf: #(!First !Second !Third)

When triggered, it randomly selects an element from anArray. No two adjacent items are ever repeats. So you can randomly access values in an array without getting the same element twice in a row.

answered May 3, 2018 by ssc (Savant) (115,850 points)
Amazing. The posibilities are endless. Thanks so much.
I really like nextRandomElementOf: algorithm , its very useful