一:出现问题的代码
//生成手机号码个数
int phonedata=${__Random(1,500,)};
StringBuffer phonedatalist = new StringBuffer();
for (int i=0;i<phonedata;i++){
////生成手机号码后8位
phonedata1=${__time(/1000000,)};
String phonedata12=String.valueOf(phonedata1);
//随机选取手机前三位
int phonedata=${__Random(0,27,)};
String[] phonelist = {"198","166","199","170","170","171","176","175","145","149","173","172","170","170","131","130","188","187","183","182","159","157","152","151","150","147","139","138"};
String phonedata2=phonelist[phonedata];
//随机选取手机第四位
int phonedata31=${__Random(0,9,)};
String phonedata311=String.valueOf(phonedata31);
//拼装成手机号码
String phone=phonedata2+phonedata311+phonedata12;
phonedatalist.append(phone).append(",");
}
二:修改之后的代码
//生成手机号码个数
int phonedata=${__Random(1,500,)};
StringBuffer phonedatalist = new StringBuffer();
Random rand = new Random();
for (int i=0;i<phonedata;i++){
//生成手机号码后8位
phonedata1=rand.nextInt(9999999) + 1000000;
String phonedata12=String.valueOf(phonedata1);
//随机选取手机前三位
int phonedata=rand.nextInt(27) + 0;
String[] phonelist = {"198","166","199","170","170","171","176","175","145","149","173","172","170","170","131","130","188","187","183","182","159","157","152","151","150","147","139","138"};
String phonedata2=phonelist[phonedata];
//随机选取手机第四位
int phonedata31=rand.nextInt(9) + 0;
String phonedata311=String.valueOf(phonedata31);
//拼装成手机号码
String phone=phonedata2+phonedata311+phonedata12;
phonedatalist.append(phone).append(",");
}
三:问题原因
1、问题还是出在随机函数定义位置上,函数定义不能放在for循环里面,而是应该在for循环外面定义好。
引用其他地方学到东西:首先就是要知道伪随机数Random的算法原理,其原理简单来说:一个种子 ---〉经过一定的算法 --〉产生一个结果(同时更改种子本身)那么.Next的时候 新的种子 ---〉经过一定的算法 --〉产生另一个结果(同时再产生一个种子)通过这样得到随机的效果,也就是说如果初始种子一样,那么后面得到的结果和顺序基本上就是一样的,