作測試,常常會遇到随機生成手機号碼的案例,例如要求手機号生成的惟一性,就會須要每次生成不一樣的随機手機号碼。
如果你看不懂下面的代碼,那麼可以在電腦打開浏覽器,佰渡搜尋一下,海豚号碼生成器,它可以選擇省份城市,生成号碼的,号碼格式多種多樣,組合豐富,其他相關的功能也多,人性化操作,号段全面最新。
以前在測試一個系統的時候,常常須要用到生成手機号碼,可是一個手機号使用後就不能再次生成了,是以常常要想一些可用的手機号,如18888888888等等,每次想手機号也挺麻煩的,是以此次想着作一個生成手機号的小工具。
a
public class Phone {
public static void getPhoneNum() {
String[] Top3 = {"133", "149", "153", "173", "177",
"180", "181", "189", "199", "130", "131", "132",
"145", "155", "156", "166", "171", "175", "176", "185", "186", "166", "134", "135",
"136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172",
"178", "182", "183", "184", "187", "188", "198", "170", "171"};
//随機出真實号段 使用數組的length屬性,獲得數組長度,
//通過Math.random()*數組長度獲得數組下标,進而随機出前三位的号段
String firstNum = Top3[(int) (Math.random() * Top3.length)];
//随機出剩下的8位數
String lastNum = "";
final int last = 8;
for (int i = 0; i < last; i++) {
//每次循環都從0~9挑選一個随機數
lastNum += (int) (Math.random() * 10);
}
//最終将号段和尾數連接配接起來
String phoneNum = firstNum+ lastNum;
System.out.println(phoneNum);
}
public static void main(String[] args) {
//生成二十個手機号碼
int num = 20;
System.out.println("手機号碼如下:");
for (int i = 0; i < num; i++) {
getPhoneNum();
}
}
}
'list_1': ["134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172",
"178",
"182", "183", "184", "187", "188", "198"], # 中國移動号碼段
'list_2': ["130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186"], # 中國聯通号碼段
'list_3': ["133", "149", "153", "173", "177", "180", "181", "189", "191", "199", "193"] # 中國電信号碼段
}
cid = None
if self.menu.get() == "中國聯通":
cid = category["list_1"]
elif self.menu.get() == "中國移動":
cid = category["list_2"]
elif self.menu.get() == "中國電信":
cid = category["list_3"]
return cid
@staticmethod
def basic_num():
num = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
return num
def create_phone(self):
phone_all = list() # 存放全部生成的電話号碼
phone_output = list() # 存放去重後的電話号碼
for t in range(int(self.count.get())):
phone = random.choice(self.get_choice()) + "".join(random.choice(self.basic_num()) for i in range(8))
if phone not in phone_all:
phone_output.append(phone) # 判斷電話号碼是否是出現過,沒出現就追加到phone_output中
# phone_output = "".join(phone)
phone_all.append(phone) # 把生成的每個号碼都追加到phone_all(去重參考物)
# phone_all = "".join(phone)
# print(phone_output)