Although Kontakt includes a built-in round-robin system in the group start settings, there are advantages to scripting your own round-robin instead. By scripting your own round-robin you have more control over how the round-robin samples are selected.
For example, maybe you want the round-robin cycle to reset automatically if nothing has been played for a few seconds. Maybe your round-robin samples are all inside of one group as described in our "Making The Most Of Groups" article. Because incrementally cycling round-robin samples can develop an audibly repeating pattern (especially when playing fast, repeated notes), sometimes a random round-robin cycle is favorable.
So how do you approach creating non-repeating random round-robin? One solution I've seen is to run a "while" loop until a randomly generated value is different than the last round-robin cycle. Although this results in a different number to use for the round-robin index every time, I'm personally not a fan of the approach. The "while" loop might terminate after one or two iterations, or it might take three or four until it resolves. While it will technically work, playing the odds with an open-ended loop isn't a very efficient, desirable route.
Here's my suggestion on how to achieve a non-repeating random round-robin cycle in a simple and efficient way:
declare polyphonic $current_round_robin
if ($total_round_robin > 1)
$current_round_robin := (%last_round_robin[$EVENT_NOTE] - 1 + random(1, $total_round_robin - 1)) mod $total_round_robin + 1
%last_round_robin[$EVENT_NOTE] := $current_round_robin
$current_round_robin := 1
As you can see, the code leverages the modulo operator in order to include all values except for the last played round-robin without the use of loops. The code sets the current round-robin variable to a number between one and the maximum round-robin available. If the round-robin value needs to be 0-index based, it's a simple adjustment.
Hopefully this approach will be useful for your own scripting projects! This approach is also fairly easy to expand on--for example handling different dynamics separately, in their own round-robin cycles.