任何国家/地区的随机 phone 号码生成器
Random phone number generator for any country
我正在尝试从国家列表中围绕 phone 个数字创建综合数据。
我发现 Google 的 libphonenumber Java 库带有 Python、C++ 等端口,是一个很好的资源。
是否可以使用此库从国家列表中生成 有效 随机 phone 号码?
使用我编写的这段代码,我正在使用他们的 getExampleNumber
函数,但它每次都生成相同的数字:
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc;
import com.google.i18n.phonenumbers.NumberParseException;
public class Driver {
public static void main(String[] args) {
// Prints "Hello, World" in the terminal window.
for (int i=0;i<10;i++)
{
System.out.println(Generator());
}
}
public static PhoneNumber Generator()
{
String regionCode = new String("GB");
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
PhoneNumber exampleNumber =phoneNumberUtil.getExampleNumber(regionCode);
return exampleNumber;
}
}
本库不为您提供随机数,无论您运行多少次您的代码总是会得到:
Country Code: 44 National Number: 1212345678
你可以看看他们的github项目:
https://github.com/googlei18n/libphonenumber/
你会看到这个文件夹里面有很多proto文件:
他们从这些文件中获得了那个数字,所以它被硬编码在那里。没办法随机获取。
最好的方法是使用库中的 REGEX,但它的工作量很大。
由于 getExampleNumber 的功能将 return 您始终是完全相同的值,您仍然可以使用它来随机化(或迭代)其中的数字。
这是一个如何操作的示例(示例 here),为每个受支持的区域获取 10 个随机移动 phone 号码:
class MainActivity : AppCompatActivity() {
@WorkerThread
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val phoneNumbersToRandomize = 10
var regionsThatFoundMobilePhonesFor = 0
if (savedInstanceState == null)
thread {
val phoneNumberUtil = PhoneNumberUtil.getInstance()
val startTime = System.currentTimeMillis()
val supportedRegions = phoneNumberUtil.supportedRegions
for (region in supportedRegions) {
val validPhoneNumbers = HashSet<String>()
val exampleNumber = phoneNumberUtil.getExampleNumberForType(region, PhoneNumberUtil.PhoneNumberType.MOBILE)
if (exampleNumber == null) {
Log.w("AppLog", "region:$region no exampleNumber")
continue
}
Log.d("AppLog", "region:$region exampleNumber:${phoneNumberUtil.format(exampleNumber, PhoneNumberUtil.PhoneNumberFormat.E164)}")
val countryCode = phoneNumberUtil.getCountryCodeForRegion(region)
val nationalSignificantNumber = phoneNumberUtil.getNationalSignificantNumber(exampleNumber)
// Log.d("AppLog", "finding $phoneNumbersToRandomize random mobile phone numbers for $region example phone number:" +
// phoneNumberUtil.format(exampleNumber, PhoneNumberUtil.PhoneNumberFormat.E164) + " countryCode:$countryCode nationalSignificantNumber:$nationalSignificantNumber ")
val randomDigitsLength = nationalSignificantNumber.length
while (validPhoneNumbers.size < phoneNumbersToRandomize) {
val inputPhone = "+$countryCode${getRandomStringOfDigits(randomDigitsLength)}"
if (validPhoneNumbers.contains(inputPhone))
continue
val phoneNumber = phoneNumberUtil.parse(inputPhone, region)
val nationalSignificantNumberFromGeneratedNumber = phoneNumberUtil.getNationalSignificantNumber(phoneNumber)
if (nationalSignificantNumberFromGeneratedNumber.length != randomDigitsLength)
continue
val isValidMobilePhoneNumber = phoneNumberUtil.isValidNumberForRegion(phoneNumber, region)
&& phoneNumberUtil.isPossibleNumberForType(phoneNumber, PhoneNumberUtil.PhoneNumberType.MOBILE)
if (isValidMobilePhoneNumber) {
validPhoneNumbers.add(inputPhone)
}
}
++regionsThatFoundMobilePhonesFor
}
Log.d("AppLog", "done regionsThatFoundMobilePhonesFor:$regionsThatFoundMobilePhonesFor out of ${supportedRegions.size}" +
"timeTaken:${System.currentTimeMillis() - startTime}ms")
}
}
companion object {
private fun getRandomStringOfDigits(numberOfDigitsToGenerate: Int): String {
val sb = StringBuilder(numberOfDigitsToGenerate)
for (i in 0 until numberOfDigitsToGenerate)
sb.append(Random.nextInt(10).toString())
return sb.toString()
}
}
}
gradle 依赖关系:
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
api 'com.googlecode.libphonenumber:libphonenumber:8.12.13'
我正在尝试从国家列表中围绕 phone 个数字创建综合数据。
我发现 Google 的 libphonenumber Java 库带有 Python、C++ 等端口,是一个很好的资源。
是否可以使用此库从国家列表中生成 有效 随机 phone 号码?
使用我编写的这段代码,我正在使用他们的 getExampleNumber
函数,但它每次都生成相同的数字:
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc;
import com.google.i18n.phonenumbers.NumberParseException;
public class Driver {
public static void main(String[] args) {
// Prints "Hello, World" in the terminal window.
for (int i=0;i<10;i++)
{
System.out.println(Generator());
}
}
public static PhoneNumber Generator()
{
String regionCode = new String("GB");
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
PhoneNumber exampleNumber =phoneNumberUtil.getExampleNumber(regionCode);
return exampleNumber;
}
}
本库不为您提供随机数,无论您运行多少次您的代码总是会得到:
Country Code: 44 National Number: 1212345678
你可以看看他们的github项目:
https://github.com/googlei18n/libphonenumber/
你会看到这个文件夹里面有很多proto文件:
他们从这些文件中获得了那个数字,所以它被硬编码在那里。没办法随机获取。
最好的方法是使用库中的 REGEX,但它的工作量很大。
由于 getExampleNumber 的功能将 return 您始终是完全相同的值,您仍然可以使用它来随机化(或迭代)其中的数字。
这是一个如何操作的示例(示例 here),为每个受支持的区域获取 10 个随机移动 phone 号码:
class MainActivity : AppCompatActivity() {
@WorkerThread
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val phoneNumbersToRandomize = 10
var regionsThatFoundMobilePhonesFor = 0
if (savedInstanceState == null)
thread {
val phoneNumberUtil = PhoneNumberUtil.getInstance()
val startTime = System.currentTimeMillis()
val supportedRegions = phoneNumberUtil.supportedRegions
for (region in supportedRegions) {
val validPhoneNumbers = HashSet<String>()
val exampleNumber = phoneNumberUtil.getExampleNumberForType(region, PhoneNumberUtil.PhoneNumberType.MOBILE)
if (exampleNumber == null) {
Log.w("AppLog", "region:$region no exampleNumber")
continue
}
Log.d("AppLog", "region:$region exampleNumber:${phoneNumberUtil.format(exampleNumber, PhoneNumberUtil.PhoneNumberFormat.E164)}")
val countryCode = phoneNumberUtil.getCountryCodeForRegion(region)
val nationalSignificantNumber = phoneNumberUtil.getNationalSignificantNumber(exampleNumber)
// Log.d("AppLog", "finding $phoneNumbersToRandomize random mobile phone numbers for $region example phone number:" +
// phoneNumberUtil.format(exampleNumber, PhoneNumberUtil.PhoneNumberFormat.E164) + " countryCode:$countryCode nationalSignificantNumber:$nationalSignificantNumber ")
val randomDigitsLength = nationalSignificantNumber.length
while (validPhoneNumbers.size < phoneNumbersToRandomize) {
val inputPhone = "+$countryCode${getRandomStringOfDigits(randomDigitsLength)}"
if (validPhoneNumbers.contains(inputPhone))
continue
val phoneNumber = phoneNumberUtil.parse(inputPhone, region)
val nationalSignificantNumberFromGeneratedNumber = phoneNumberUtil.getNationalSignificantNumber(phoneNumber)
if (nationalSignificantNumberFromGeneratedNumber.length != randomDigitsLength)
continue
val isValidMobilePhoneNumber = phoneNumberUtil.isValidNumberForRegion(phoneNumber, region)
&& phoneNumberUtil.isPossibleNumberForType(phoneNumber, PhoneNumberUtil.PhoneNumberType.MOBILE)
if (isValidMobilePhoneNumber) {
validPhoneNumbers.add(inputPhone)
}
}
++regionsThatFoundMobilePhonesFor
}
Log.d("AppLog", "done regionsThatFoundMobilePhonesFor:$regionsThatFoundMobilePhonesFor out of ${supportedRegions.size}" +
"timeTaken:${System.currentTimeMillis() - startTime}ms")
}
}
companion object {
private fun getRandomStringOfDigits(numberOfDigitsToGenerate: Int): String {
val sb = StringBuilder(numberOfDigitsToGenerate)
for (i in 0 until numberOfDigitsToGenerate)
sb.append(Random.nextInt(10).toString())
return sb.toString()
}
}
}
gradle 依赖关系:
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
api 'com.googlecode.libphonenumber:libphonenumber:8.12.13'