Generates a random string.

    :param length: Length of the string to generate.
                   This can be a numbe or a pair: ``(min_length, max_length)``
    :param chars: String of characters to choose from
331271
def random(length, chars=None):
    
    if chars is None:
        chars = string.ascii_letters + string.digits
    else:
        ensure_string(chars)
        if not chars:
            raise ValueError("character set must not be empty")

    if is_pair(length):
        length = randint(*length)
    elif isinstance(length, Integral):
        if not length > 0:
            raise ValueError(
                "random string length must be positive (got %r)" % (length,))
    else:
        raise TypeError("random string length must be an integer; "
                        "got '%s'" % type(length).__name__)

    return join(chars.__class__(), (choice(chars) for _ in xrange(length)))
1039713
def generate(self, chars, format='png'):
        
        im = self.generate_image(chars)
        out = BytesIO()
        im.save(out, format=format)
        out.seek(0)
        return out
1236140
public static String createRandomNum(int length) {
        if (length < 0) {
            throw new IllegalArgumentException("长度不能小于0");
        }
        char[] chars = new char[length];
        for (int i = 0; i < length; i++) {
            chars[i] = (char) ((Math.random() * 10) + 48);
        }
        return new String(chars);
    }
488851
def random(self, length=22):
        
        random_num = int(binascii.b2a_hex(os.urandom(length)), 16)
        return self._num_to_string(random_num, pad_to_length=length)[:length]
408005
def length_range(string, minimum, maximum):
    
    int_range(len(string), minimum, maximum)
    return string
575778
function randomString(_string_length, _mode) {
    
    var string_length = _string_length;
    if(string_length < 3)
    	string_length = 2;
    
    var chars = chars_all;
    
    if(_mode === 'lower_case') chars = chars_lower;
    else if(_mode === 'upper_case') chars = chars_upper;
    else if(_mode === 'nums_oly') chars = chars_nums;
    else if(_mode === 'especials') chars = chars_all + chars_spec;
    
    var str = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        str += chars.substring(rnum,rnum+1);
    }
    
    return str;
}
1058702
def gen_random_string(str_len):
    
    return ''.join(
        random.choice(string.ascii_letters + string.digits) for _ in range(str_len))
207821
public static String randomString(int _length) {
        if (_length <= 0) {
            return "";
        }
        Random random = new Random();
        char[] buf = new char[_length];
        for (int idx = 0; idx < buf.length; ++idx)
            buf[idx] = SYMBOLS[random.nextInt(SYMBOLS.length)];
        return new String(buf);
    }
348753
def rand_hexstr(length, lower=True):
    
    if lower:
        return rand_str(length, allowed=CHARSET_HEXSTR_LOWER)
    else:
        return rand_str(length, allowed=CHARSET_HEXSTR_UPPER)
966475
def random_str_uuid(string_length):
    """"""
    if not isinstance(string_length, int) or not 1 <= string_length <= 32:
        msg = "string_length must be type int where 1 <= string_length <= 32"
        raise ValueError(msg)
    random = str(uuid.uuid4()).upper().replace('-', '')
    return random[0:string_length]
228328
public static function get_random_string($length = 10, $chars)
	{
	    $random_string = '';
	    
	    for($i = 0; $i < $length; $i++)
	    {
	        $index = rand(0, strlen($chars) - 1);
	        $random_string .= $chars{$index};
	    }
	    
	    return $random_string;
	}
1237292
protected function rand($chars, $length)
    {
        $string = '';
        $max    = $this->len($chars) - 1;

        $i = 0;
        while($i < $length)
        {
            $string .= $chars[\random_int(0, $max)];
            $i++;
        }

        return $string;
    }
737917
def gen_secret(length=64):
    
    charset = string.ascii_letters + string.digits
    return ''.join(random.SystemRandom().choice(charset)
                   for _ in range(length))
1037668
def _random_string():
    
    rng = random.SystemRandom()
    token_value = u''.join(rng.choice(CHARSET) for _ in range(CHAR_GROUPS * CHARS_PER_GROUP))
    if CHARS_PER_GROUP > 1:
        return GROUP_SEP.join(map(u''.join, zip(*[iter(token_value)] * CHARS_PER_GROUP)))
    else:
        return token_value
1149636
public static String generateDigitsString(int length) {
        StringBuffer buf = new StringBuffer(length);
        SecureRandom rand = new SecureRandom();
        for (int i = 0; i < length; i++) {
            buf.append(rand.nextInt(digits.length));
        }
        return buf.toString();
    }
294265
def _join_chars(chars, length):
    
    mult = int(length / len(chars)) + 1
    mult_chars = chars * mult
    return "".join(random.sample(mult_chars, length))
332199
public static String comicChars (int length)
    {
        char[] chars = new char[length];
        for (int ii=0; ii < length; ii++) {
            chars[ii] = RandomUtil.pickRandom(COMIC_CHARS);
        }
        return new String(chars);
    }