Bootstrap

postman生成随机手机号

function getMoblieNum(){
var numArray = new Array("139","138","137","136","135","134","159","158","157","150","151","152","188","187","182","183","184","178","130","131","132","156","155","186","185","176","133","153","189","180","181","177");  //这是目前找到的除了数据卡外的手机卡前三位,类型是字符串数组
var arraryLength = numArray.length;  //获取数组长度,这样如果手机号前三位取值单位发生变化,在下一步求i的地方就不用修改随机数取值范围了
var i = parseInt( Math.random() * arraryLength); //注意乘以的是上面numArray数组的长度,这样就可以取出数组中的随机一个数。random的取值范围是大于等于0.0,小于1.0,相乘后得到的就是0到(数组长度-1)的值。
var num = numArray[i];  //取出随机的手机号前三位并赋值给num,手机号前三位是字符串类型的
for ( var j = 0; j < 8; j++){
num = num + Math.floor(Math.random() * 10);   //num是字符串,后面的数字被当做字符串。所以变成两个字符串拼接了
}
return num
}

// 生成随机手机号,并存入变量中
pm.globals.set("phone", getMoblieNum());

生成随机字符串

//产生随机字符串
function randomString(length, chars) {
    var result = '';
    for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
    return result;
}
 
// 调用方法生成随机字符串
var rString = randomString(8, '1234567890');
// 把生成的数据存入变量
pm.globals.set("userName", rString);

;