{
    "mode": "perldoc",
    "parameter": "String::Random",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/String%3A%3ARandom/json",
    "generated": "2026-06-11T12:20:32Z",
    "synopsis": "use String::Random;\nmy $stringgen = String::Random->new;\nprint $stringgen->randregex('\\d\\d\\d'); # Prints 3 random digits\n# Prints 3 random printable characters\nprint $stringgen->randpattern(\"...\");\n*or*\nuse String::Random qw(randomregex randomstring);\nprint randomregex('\\d\\d\\d'); # Also prints 3 random digits\nprint randomstring(\"...\");   # Also prints 3 random printable characters",
    "sections": {
        "NAME": {
            "content": "String::Random - Perl module to generate random strings based on a pattern\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 0.32\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use String::Random;\nmy $stringgen = String::Random->new;\nprint $stringgen->randregex('\\d\\d\\d'); # Prints 3 random digits\n# Prints 3 random printable characters\nprint $stringgen->randpattern(\"...\");\n\n*or*\n\nuse String::Random qw(randomregex randomstring);\nprint randomregex('\\d\\d\\d'); # Also prints 3 random digits\nprint randomstring(\"...\");   # Also prints 3 random printable characters\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module makes it trivial to generate random strings.\n\nAs an example, let's say you are writing a script that needs to generate a random password for a\nuser. The relevant code might look something like this:\n\nuse String::Random;\nmy $pass = String::Random->new;\nprint \"Your password is \", $pass->randpattern(\"CCcc!ccn\"), \"\\n\";\n\nThis would output something like this:\n\nYour password is UDwp$tj5\n\nNOTE!!!: currently, \"String::Random\" defaults to Perl's built-in predictable random number\ngenerator so the passwords generated by it are insecure. See the \"randgen\" option to\n\"String::Random\" constructor to specify a more secure random number generator. There is no\nequivalent to this in the procedural interface, you must use the object-oriented interface to\nget this functionality.\n\nIf you are more comfortable dealing with regular expressions, the following code would have a\nsimilar result:\n\nuse String::Random;\nmy $pass = String::Random->new;\nprint \"Your password is \",\n$pass->randregex('[A-Z]{2}[a-z]{2}.[a-z]{2}\\d'), \"\\n\";\n",
            "subsections": [
                {
                    "name": "Patterns",
                    "content": "The pre-defined patterns (for use with \"randpattern()\" and \"randompattern()\") are as follows:\n\nc        Any Latin lowercase character [a-z]\nC        Any Latin uppercase character [A-Z]\nn        Any digit [0-9]\n!        A punctuation character [~`!@$%^&*()-+={}[]|\\:;\"'.<>?/#,]\n.        Any of the above\ns        A \"salt\" character [A-Za-z0-9./]\nb        Any binary data\n\nThese can be modified, but if you need a different pattern it is better to create another\npattern, possibly using one of the pre-defined as a base. For example, if you wanted a pattern\n\"A\" that contained all upper and lower case letters (\"[A-Za-z]\"), the following would work:\n\nmy $gen = String::Random->new;\n$gen->{'A'} = [ 'A'..'Z', 'a'..'z' ];\n\n*or*\n\nmy $gen = String::Random->new;\n$gen->{'A'} = [ @{$gen->{'C'}}, @{$gen->{'c'}} ];\n\n*or*\n\nmy $gen = String::Random->new;\n$gen->setpattern(A => [ 'A'..'Z', 'a'..'z' ]);\n\nThe randomstring function, described below, has an alternative interface for adding patterns.\n"
                },
                {
                    "name": "Methods",
                    "content": "new\nnew max => *number*\nnew randgen => *sub*\nCreate a new String::Random object.\n\nOptionally a parameter \"max\" can be included to specify the maximum number of characters\nto return for \"*\" and other regular expression patterns that do not return a fixed\nnumber of characters.\n\nOptionally a parameter \"randgen\" can be included to specify a subroutine coderef for\ngenerating the random numbers used in this module. The coderef must accept one argument\n\"max\" and return an integer between 0 and \"max - 1\". The default randgen coderef is\n\nsub {\nmy ($max) = @;\nreturn int rand $max;\n}\n\nrandpattern LIST\nThe randpattern method returns a random string based on the concatenation of all the\npattern strings in the list.\n\nIt will return a list of random strings corresponding to the pattern strings when used\nin list context.\n\nrandregex LIST\nThe randregex method returns a random string that will match the regular expression\npassed in the list argument.\n\nPlease note that the arguments to randregex are not real regular expressions. Only a\nsmall subset of regular expression syntax is actually supported. So far, the following\nregular expression elements are supported:\n\n\\w    Alphanumeric + \"\".\n\\d    Digits.\n\\W    Printable characters other than those in \\w.\n\\D    Printable characters other than those in \\d.\n.     Printable characters.\n[]    Character classes.\n{}    Repetition.\n*     Same as {0,}.\n?     Same as {0,1}.\n+     Same as {1,}.\n\nRegular expression support is still somewhat incomplete. Currently special characters\ninside [] are not supported (with the exception of \"-\" to denote ranges of characters).\nThe parser doesn't care for spaces in the \"regular expression\" either.\n\ngetpattern STRING\nReturn a pattern given a name.\n\nmy $gen = String::Random->new;\n$gen->getpattern('C');\n\n(Added in version 0.32.)\n\nsetpattern STRING ARRAYREF\nAdd or redefine a pattern given a name and a character set.\n\nmy $gen = String::Random->new;\n$gen->setpattern(A => [ 'A'..'Z', 'a'..'z' ]);\n\n(Added in version 0.32.)\n\nfrompattern\nIGNORE! - for compatibility with an old version. DO NOT USE!\n"
                },
                {
                    "name": "Functions",
                    "content": "randomstring PATTERN,LIST\nrandomstring PATTERN\nWhen called with a single scalar argument, randomstring returns a random string using\nthat scalar as a pattern. Optionally, references to lists containing other patterns can\nbe passed to the function. Those lists will be used for 0 through 9 in the pattern\n(meaning the maximum number of lists that can be passed is 10). For example, the\nfollowing code:\n\nprint randomstring(\"0101\",\n[\"a\", \"b\", \"c\"],\n[\"d\", \"e\", \"f\"]), \"\\n\";\n\nwould print something like this:\n\ncebd\n\nrandomregex REGEXINSTRING\nPrints a string for the regular expression given as the string. See the synposis for\nexample.\n"
                }
            ]
        },
        "BUGS": {
            "content": "Please report any bugs or feature requests on the bugtracker website\n<https://github.com/shlomif/string-random/issues>\n\nWhen submitting a bug or request, please include a test-file or a patch to an existing test-file\nthat illustrates the bug or desired feature.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Shlomi Fish <shlomif@cpan.org>\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "",
            "subsections": [
                {
                    "name": "perl",
                    "content": ""
                }
            ]
        },
        "SUPPORT": {
            "content": "",
            "subsections": [
                {
                    "name": "Websites",
                    "content": "The following websites have more information about this module, and may be of help to you. As\nalways, in addition to those websites please use your favorite search engine to discover more\nresources.\n\n*   MetaCPAN\n\nA modern, open-source CPAN search engine, useful to view POD in HTML format.\n\n<https://metacpan.org/release/String-Random>\n\n*   RT: CPAN's Bug Tracker\n\nThe RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.\n\n<https://rt.cpan.org/Public/Dist/Display.html?Name=String-Random>\n\n*   CPANTS\n\nThe CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.\n\n<http://cpants.cpanauthors.org/dist/String-Random>\n\n*   CPAN Testers\n\nThe CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN\ndistributions.\n\n<http://www.cpantesters.org/distro/S/String-Random>\n\n*   CPAN Testers Matrix\n\nThe CPAN Testers Matrix is a website that provides a visual overview of the test results for\na distribution on various Perls/platforms.\n\n<http://matrix.cpantesters.org/?dist=String-Random>\n\n*   CPAN Testers Dependencies\n\nThe CPAN Testers Dependencies is a website that shows a chart of the test results of all\ndependencies for a distribution.\n\n<http://deps.cpantesters.org/?module=String::Random>\n\nBugs / Feature Requests\nPlease report any bugs or feature requests by email to \"bug-string-random at rt.cpan.org\", or\nthrough the web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=String-Random>.\nYou will be automatically notified of any progress on the request by the system.\n"
                },
                {
                    "name": "Source Code",
                    "content": "The code is open to the world, and available for you to hack on. Please feel free to browse it\nand play with it, or whatever. If you want to contribute patches, please send me a diff or prod\nme to pull from your repository :)\n\n<https://github.com/shlomif/string-random>\n\ngit clone http://github.com/shlomif/String-Random\n"
                }
            ]
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2021 by Shlomi Fish.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "String::Random - Perl module to generate random strings based on a pattern",
    "flags": [],
    "examples": [],
    "see_also": []
}