venerdì 26 giugno 2015

MEL scripting - array decreasing procedure with random selection

I'm sharing a small algorithm I developed for the last production I've been working on. I needed to randomly select characters in a scene and assign a value, but I wanted to avoid to occasionally select twice the same character.

This procedure randomly select an element in an array and delete it so that the array size decrease until all elements are gone and the array is empty.


proc randDecreasArray(){
    string $arr[] = {"a","b","c","d","e","f","g"};
    string $nwArr[] = $arr;

    int $sz = size($arr);
    int $rm = $sz;

    $prntArr = stringArrayToString($arr, ".");
    print ($prntArr + "\n");

    for ($i in $arr){

        int $rnd = rand(0, $rm);
        $nwArr = stringArrayRemove ({$nwArr[$rnd]},$nwArr);
        $rm --;
        $prnt = stringArrayToString($nwArr, ".");
        print ($prnt + "\n");