viewing paste topic/4889- shuffle algorithm | Athena

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//  callfunc "shuffle", <min num>, <max num>, <output array> {, <count> };
function    script  shuffle__   {
    .@static = getarg(0);
    .@range = getarg(1) +1 - .@static;
    .@count = getarg(3, 0);
    if ( .@range <= 0 )
        return 0;
    if ( !.@count || .@count > .@range )
        .@count = .@range;
    while ( .@i < .@count ) {
        .@r = .@save = rand( .@i, .@range -1 ) ;
        if ( !.@tmp1[.@i] ) {
            .@r = .@tmp1[.@r] ? .@tmp2[.@r] : .@r;
            .@tmp2[.@i] = .@r;
            .@tmp2[.@save] = .@i;
            .@tmp1[.@save] = 1;
            set getelementofarray( getarg(2), .@i ), .@r + .@static;
            if ( .@save < .@count )
                set getelementofarray( getarg(2), .@save ), .@i + .@static;
        }
        .@i++;
    }
    return .@count;
}
/*
prontera,156,184,5  script  kjdshfksfjh 100,{
//  input .@min, -1000000000, 1000000000;
//  input .@max, -1000000000, 1000000000;
    .@min = 1;
    .@max = 5;
    callfunc "shuffle__", .@min, .@max, .@output$, 100;
    dispbottom implode( .@output$, "," );
}
*/
Viewed 1248 times, submitted by AnnieRuru.