随机生成字符串

1.相关的随机函数:

mt_rand(),shuffle(),str_shuffle()

2.总之就是字符串 数组 的灵活使用

#打乱字符串str_shuffle, 截取字符串substr

$str="abcdefghjklmnuvwxyz0123456789";
        $str = str_shuffle($str);
        echo substr($str,0,8);

#看别人写的随机生成串的函数

产生字符串的长度-》随机得到数据的小标》取数组的值拼接就ok

public static function make_rand_string($len, $include_number = false) {
        $string_list = array (‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘ );

        $string_include_number_list = array (‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘M‘, ‘N‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ );

        if ($include_number) {
            $arr = $string_include_number_list;
        } else {
            $arr = $string_list;
        }

        $rand_max = count ( $arr ) - 1;
        $return_string = null;

        for($i = 0; $i < $len; $i ++) {
            $rand = mt_rand ( 0, $rand_max );
            $return_string .= $arr [$rand];
        }

        return $return_string;

    }
时间: 2024-08-15 06:50:15

随机生成字符串的相关文章

mkpasswd命令(用来随机生成字符串)

shell脚本时可以用到随机生成字符串的工具(可以输入命令后出来的字符串当作密码使用):mkpasswd(make passwd)安装包命令:yum install -y expect 命令:mkpasswd 指定长度:mkpasswd -l +字符串大小(数字)例:mkpasswd -l 12 指定有几个特殊符号:mkpasswd -l +字符串大小(数字) -s 特殊符号数量(数字)例:mkpasswd -l 12 -s 3(12位有三个特殊符号) 原文地址:http://blog.51ct

随机生成字符串,可用来当id

// 随机生成字符串RandomNumb(n) { let str = 'abcdefghijklmnopqrstuvwxyz9876543210'; let tmp = '', i = 0, l = str.length; for (i = 0; i < n; i++) { tmp += str.charAt(Math.floor(Math.random() * l)); } return tmp;}var id = vm.RandomNumb(20)console.log(id) 原文地址:

Horspool算法(java)随机生成字符串

java代码 import java.util.Scanner; public class Horspool { public static void ShiftTable(char[] p, int[] table){ for (int i = 0; i < 26; i++) { table[i] = p.length; } for (int i = 0; i < p.length - 1; i++) { table[p[i] -'A'] = p.length - 1 - i; } } pu

ruby随机生成字符串

随机生成一个固定位数的字符串: def newpass( len ) chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a newpass = "" 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] } return newpass

java中随机生成字符串的方法(三种)

1.生成的字符串每个位置都有可能是str中的一个字母或数字,需要导入的包是import java.util.Random; //length用户要求产生字符串的长度 public static String getRandomString(int length){ String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random=new Random(); StringB

随机生成字符串方法

1 package beifeng.hadoop; 2 3 import java.util.Random; 4 import org.apache.commons.lang.RandomStringUtils; 5 6 /** 7 * Three Methods to generate random string. 8 */ 9 10 public class RandomString { 11 /** 12 * 生成的字符串每个位置都有可能是str中的一个字母或数字,需要导入的包是impor

java随机生成字符串工具类

package aA; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; /** * 字符随机生成类 */ public class RandomDemo { /** * 随机产生类型枚举 */ public static enum TYPE { /**小字符型*/ LETTER, /**大写字符型*/ CAPITAL, /**数字型*/ NUMBER, /**大+小字符 型*/ LETTE

PHP随机生成字符串

当我们需要生成一个随机名字,临时密码等字符串时可以用到下面的函数 function generateRandomString($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0,

随机生成字符串,数字,手机号,邮箱

/** * echo nRand('mail');die; * @param array $type * @param int $len * @return int|string */function nRand($type=[],$len= 20){ $t = gettype($type); if($t === 'string'){ $rand=""; if($type == 'phone') { $rand=0; for ($i = 1; $i < 9; ++$i) { $r