generates a secure random string
@param  integer $token_length The number of characters including the prefix
@param  string  $prefix       A prefix for the random string
@return string                The random string starting with prefix
3831
public function generateToken($token_length=30, $prefix='') {
        $token_length = $token_length - strlen($prefix);
        if ($token_length < 0) { throw new Exception("Prefix is too long", 1); }
        
        $token = "";
        while (($len = strlen($token)) < $token_length) {
            $remaining_len = $token_length - $len;
            $bytes = random_bytes($remaining_len);
            $token .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $remaining_len);
        }

        return $prefix.$token;
    }
1079061
public static function generate($length=128) {

        if ($length < 32) {

            return substr(self::getUid(), 0, $length);

        } else if ($length == 32) {

            return self::getUid();

        }  else {

            $numString = (int)($length/32) + 1;
            $randNum = "";
            for ($i = 0; $i < $numString; $i++) $randNum .= self::getUid();

            return substr($randNum, 0, $length);

        }

    }
925852
public static function create_guid_sha256($prefix=null){
        // mt_rand() 马特赛特旋转演算法，可以快速产生高质量的伪随机数，修正了古老随机数产生算法的很多缺陷
        return strtolower(hash_hmac("sha256",uniqid($prefix . mt_rand(), true), self::$_secret4sha265));
    }
255413
public function generateAlphaNumToken($length)
    {
        $characters = str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');

        srand((float)microtime() * 1000000);

        $token = '';

        do
        {
            shuffle($characters);
            $token .= $characters[mt_rand(0, (count($characters) - 1))];
        } while (strlen($token) < $length);

        return $token;
    }
356461
public static String generateSecurityToken(int length, boolean urlSafe) {
		final byte[] bytes = new byte[length];
		SecureRandom rand;
		try {
			rand = SecureRandom.getInstance("SHA1PRNG");
		} catch (NoSuchAlgorithmException ex) {
			logger.error(null, ex);
			rand = new SecureRandom();
		}
		rand.nextBytes(bytes);
		return urlSafe ? base64encURL(bytes) : base64enc(bytes);
	}
444936
static public function generateUniqidString($prefix = '', $length = 20)
    {
        $key = '';
        $keys = array_merge(range(0, 9), range('a', 'z'));

        for ($i = 0; $i < $length; $i++) {
            $key .= $keys[array_rand($keys)];
        }

        return uniqid($prefix) . $key;
    }
1236134
private function _getRandomKey($length = 256)
	{
		$key = '';
		$salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$saltLength = strlen($salt);

		// Build the random key.
		for ($i = 0; $i < $length; $i++)
		{
			$key .= $salt[mt_rand(0, $saltLength - 1)];
		}

		return $key;
	}
659547
public function generate($length)
    {
        $realPool = $this->mergePools($this->characterPools);
        $max = (count($realPool) - 1);
        mt_srand();

        $randomString = '';

        for ($i = 0; $i < $length; $i++) {
            $randomString .= $realPool[mt_rand(0, $max)];
        }

        return $randomString;
    }
743789
private function randomToken($length = 32)
    {
        if (!isset($length) || intval($length) <= 8) {
            $length = 32;
        }
        if (function_exists('random_bytes')) {
            return random_bytes($length);
        }
        if (function_exists('mcrypt_create_iv')) {
            return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
        }
        if (function_exists('openssl_random_pseudo_bytes')) {
            return openssl_random_pseudo_bytes($length);
        }

        return @crypt(uniqid());
    }
58141
public function getRandomString(int $length = 64): string
    {
        $chars  = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()';
        $random = '';

        for ($i = 0; $i < $length; $i++) {
            $random .= substr($chars, random_int(0, strlen($chars) - 1), 1);
        }

        return $random;
    }
600926
public static function random(string $pool = Str::ALNUM, int $length = 32): string
	{
		$string = '';

		$poolSize = mb_strlen($pool) - 1;

		for($i = 0; $i < $length; $i++)
		{
			$string .= mb_substr($pool, random_int(0, $poolSize), 1);
		}

		return $string;
	}
955943
public static function generateCustom($prefix, $length=128) {

        if ( $length <= (strlen($prefix)+1) ) {
            throw new InvalidArgumentException("Uid length cannot be smaller than prefix length +1");

        }

        return "$prefix-".self::generate($length-(strlen($prefix)+1));

    }
910054
static function generate($length = 10)
    {
        $chars = "A1B2C3D4E5F6G7H8I9J0K1L2M3N4O5P6Q7R8S9T0U1V2W3X4Y5Z6a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6";
        $key = "";

        for ($i = 0; $i < $length; $i++) {
            $key .= $chars{mt_rand(0, strlen($chars) - 1)};
        }

        return $key;
    }
420184
protected String generateToken() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < TOKEN_MIDDLE_LEN; i++) {
            sb.append(TOKEN_DICT.charAt(random.nextInt(TOKEN_DICT.length())));
        }
        return TOKEN_PREFIX + sb.toString() + TOKEN_SUFFIX;
    }
1294007
public static function random($length) {
        $res = '';
        for ($i = 0; $i < $length; $i++) {
            $res .= chr(mt_rand(33, 126));
        }
        return $res;
    }
109694
public static function random($charLists = null, $length = 32)
    {
        if (!$charLists) {
            $charLists = static::ALNUM;
        }

        $string   = '';
        $poolSize = mb_strlen($charLists) - 1;

        for ($i = 0; $i < $length; $i++) {
            $string .= mb_substr($charLists, mt_rand(0, $poolSize), 1);
        }

        return $string;
    }
447686
def generate_random_token(length=32):
    
    chars = (string.ascii_lowercase + string.ascii_uppercase + string.digits)
    return ''.join(random.choice(chars) for _ in range(length))