You are currently viewing the GMLscripts.com static mirror. Forum access and script submissions are not available through this mirror.

Invert GMLscripts.com

string_random

Returns a string of a given length composed of characters randomly selected from a given set. If the set is empty, the set of all printable ASCII characters will be used.

NOTE: This differs from the legacy version which could select from 256 possible characters. GameMaker's current null-terminated string handling would cause it to fail if character 0 was selected. The new default behavior using printable ASCII is probably more useful. If you need to produce random byte values, a buffer should be used.

string_random(str, count)
Returns a string of a given length composed of characters randomly selected from a given set.
COPY/// @func   string_random(str, count)
///
/// @desc   Returns a string of a given length composed of characters 
///         randomly selected from a given set. If the set is empty, 
///         the set of all printable ASCII characters will be used.
///
/// @param  {string}    str         set of characters, optional
/// @param  {real}      count       number of characters to return
///
/// @return {string}    random string
///
/// GMLscripts.com/license

function string_random(str, count)
{
    var out = "";
    var len = string_length(str);
    if (len > 0) {
        repeat (count) {
            out += string_char_at(str, floor(random(len)) + 1);
        }
    } else {
        repeat (count) {
            out += chr(floor(random(95)) + 32);
        }
    }
    return out;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw