方法一 随机数生成方法,使用BeanShell 预处理程序
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("phone", yy);
2、调用随机数
3、运行查看结果数,随机生成11位数字
方法二 利用时间戳+随机数生成手机号后八位,减少重复(但是手机号前三位只能固定写死)
1、获取时间戳前八位
取时间戳前x位数:${__time(/1【后面带(13-x)个0】,)}
时间戳前八位:${__time(/100000,)}
2、【线程组-添加-后置处理器-BeanShell 预处理程序】
代码如下:
//${__time(/100000,)}也可由函数助手生成,${__Random(1,100000,)}随机生成数,与时间戳相加,避免重复
phone=${__time(/100000,)}+${__Random(1,100000,)};
//phone是一个整数,这里将其转化为一个字符串并且赋给a
String a=String.valueOf(phone);
//将a设置为变量
vars.put("phone",a);
//在debug sample中可查看phone的取值
vars.get("phone");
3、调用后八位随机数
4、运行查看结果数,随机生成187开头的11位数字