GameMaker: String shuffling

Shuffling string characters in GameMaker

This minimalistic example provides you with a function to shuffle strings. That is, returned string contains all characters from source string, but mixed in random order.

Download

Code follows,

/// string_shuffle(string)
// Returns a string with contents shuffled.
var r, i;
r = '';
i = 1;
// for each character in source,
repeat (string_length(argument0)) {
    // insert character at random
    // position into destination string
    r = string_insert(
        string_char_at(argument0, i),
        r, irandom(i));
    i += 1;
}
return r;

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.