(一)随机数生成方法
1、BeanShell 预处理程序
//定义手机号前3位 String[] FirstThreeNumber = {"134","135","136","137","138","139","147","150","152","157","158","159","172","178","182","183","184","187","188","198","130","131","132","145","155","156","166","171","175","176","185","186","166","133","149","153","173","177","180","181","189","199"}; //随机获取前三位手机号的index int n= (int)(Math.random() * FirstThreeNumber.length); //获取手机前三位 String yy = FirstThreeNumber[n]; //循环获取手机号后8位 for(int i = 0; i < 8; i++){ int x = (int)(Math.random() * 9); yy = yy + x; }; vars.put("com_phone", yy);
调用随机数
2、BeanShell 预处理程序,利用时间戳+随机数生成手机号,减少重复(注:图中用了后置处理程序错误,并且只能固定前三位使用,并且暂时不知json格式如何做到字符与变量一起传递)
(二)生成随机中文字符
1、利用函数RandomString
${__RandomString(1,赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕)}${__RandomString(2,文林欣德和泽子墨鸿哲兴贤良翰翰池景澄乐成俊)}
2、BeanShell 预处理程序
import java.util.Random; public class Random_str { public static String RandomJianHan(int len) { String ret = ""; for (int i = 0; i < len; i++) { String str = null; int hightPos, lowPos; // 定义高低位 Random random = new Random(); hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值 lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值 byte[] b = new byte[2]; b[0] = (new Integer(hightPos).byteValue()); b[1] = (new Integer(lowPos).byteValue()); try { str = new String(b, "GBk"); // 转成中文 } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } ret += str; } return ret; } } Random_str ran = new Random_str(); String companyname = ran.RandomJianHan(5);//此处生成的是长度为5的字符串 vars.put("companyname",companyname); Random_str ran = new Random_str(); String content1 = "随机生成内容:"+ran.RandomJianHan(10);//此处生成的是长度为10的字符串