Python randomly generates cell phone numbers

Source: Internet
Author: User
Basis
According to the October 2017 latest mobile phone number is coded, is the following:
(13\d|14[579]|15[^4\d]|17[^49\d]|18\d) \d{8}
Code
#-*-Coding:utf-8-*-

import random


def create_phone ():
    # second digit
    second = [3, 4, 5, 7, 8][random.randint (  0, 4)]

    # third digit
    third = {
        3:random.randint (0, 9),
        4: [5, 7, 9][random.randint (0, 2)],
        5: [I for I in Range (a) if I!= 4][random.randint (0, 8)],
        7: [I for I in range (ten) if I not in [4, 9]][random.randint (0, 7)],
  
   8:random.randint (0, 9),
    }[second]

    # last eight digits
    suffix = random.randint (9999999,100000000)

    # splicing cell phone number Return
    "1{}{}{}". Format (second, third, suffix)

# Generate phone number of mobile
= Create_phone ()
print (phone)
  
Run Results
13937342780
15835720604
14589505530
...
validation (using regular authentication)
#-*-Coding:utf-8-*-

import random
import re


def create_phone ():
    # second digit
    second = [3, 4, 5, 7, 8][r Andom.randint (0, 4)]

    # third digit
    third = {
        3:random.randint (0, 9),
        4: [5, 7, 9][random.randint (0, 2)],
        5: [I for I in range (Ten) if I!= 4][random.randint (0, 8)],
        7: [I for I in range (ten) if I not in [4, 9]][random.randin T (0, 7)],
        8:random.randint (0, 9),
    }[second]

    # last eight digits
    suffix = random.randint (9999999,100000000)

    # splicing Mobile phone number return
    "1{}{}{}". Format (second, third, suffix

) # Generate mobile phone number
= Create_phone (
) print (phone)

# regular
reg = Re.compile ("(13\d|14[579]|15[^4\d]|17[^49\d]|18\d) \d{8}")
print ("Test passed!" if Reg.match ( Phone) Else "Test failed!")
Validation Results
18662182464
Test passed!

15896505277
Test passed!

14952715286
Test passed!

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.