Tunechi
Tunechi

Reputation: 61

Making random phone number xxx-xxx-xxxx

It will return a random phone number xxx-xxx-xxxx with the following restrictions:

Upvotes: 3

Views: 25071

Answers (4)

Yanuar Kusnadi
Yanuar Kusnadi

Reputation: 243

I tried to combine OP,@kgull,@Cyber's code and @ivan_pozdeev's concern, also fulfill OP's requirement :

>>> def gen_phone():
    first = str(random.randint(100,999))
    second = str(random.randint(1,888)).zfill(3)

    last = (str(random.randint(1,9998)).zfill(4))
    while last in ['1111','2222','3333','4444','5555','6666','7777','8888']:
        last = (str(random.randint(1,9998)).zfill(4))
        
    return '{}-{}-{}'.format(first,second, last)

>>> for _ in xrange(10):
    gen_phone()
    
'496-251-8419'
'102-665-1932'
'262-624-5025'
'230-459-3242'
'355-131-0243'
'488-001-6828'
'244-539-2369'
'896-547-4539'
'522-406-8256'
'789-373-4240'

Upvotes: 10

Danny Denenberg
Danny Denenberg

Reputation: 49

This is my clean and simple answer.

It allows you to generate a random phone number in a random format.

def generate_phone_number():
    formats = [
        "({}{}{}) {}{}{}-{}{}{}{}",
        "{}{}{}{}{}{}{}{}{}{}",
        "({}{}{})-{}{}{}-{}{}{}{}",
    ]

    ten_numbers = [random.randint(0, 9) for _ in range(10)]
    return random.choice(formats).format(*ten_numbers)

Upvotes: 0

Kyle G
Kyle G

Reputation: 1048

Slightly simpler solution.

import random

def phn():
    n = '0000000000'
    while '9' in n[3:6] or n[3:6]=='000' or n[6]==n[7]==n[8]==n[9]:
        n = str(random.randint(10**9, 10**10-1))
    return n[:3] + '-' + n[3:6] + '-' + n[6:]

And a solution that returns the first time, every time (no while loops).

import random

def phn():
    p=list('0000000000')
    p[0] = str(random.randint(1,9))
    for i in [1,2,6,7,8]:
        p[i] = str(random.randint(0,9))
    for i in [3,4]:
        p[i] = str(random.randint(0,8))
    if p[3]==p[4]==0:
        p[5]=str(random.randint(1,8))
    else:
        p[5]=str(random.randint(0,8))
    n = range(10)
    if p[6]==p[7]==p[8]:
        n = (i for i in n if i!=p[6])
    p[9] = str(random.choice(n))
    p = ''.join(p)
    return p[:3] + '-' + p[3:6] + '-' + p[6:]

Upvotes: 6

Cory Kramer
Cory Kramer

Reputation: 117981

import random

Let's start with the area code. No leading zero, so only pick between 1 and 9. Then the remaining two can be anything between 00 and 99.

def makeFirst():
    first_digit = random.randint(1,9)
    remaining = random.randint(0,99)
    return first_digit*100 + remaining

Next the middle numbers. They cannot have a 9 so sample 0 to 8. Then loop until you get a valid case, throwing out if you happen to sample a 000.

def makeSecond():
    middle = 0
    while middle == 0:
        middle1 = random.randint(0,8)
        middle2 = random.randint(0,8)
        middle3 = random.randint(0,8)
        middle = 100*middle1 + 10*middle2 + middle3
    return middle

For the last four numbers, we'll use random.sample to ensure that we don't get any repeats.

def makeLast():
    return ''.join(map(str, random.sample(range(10),4)))

Finally join the whole thing together and format it like a phone number.

def makePhone():
    first = makeFirst()
    second = makeSecond()
    last = makeLast()
    return '{3}-{3}-{4}'.format(first,second,last)

A few tests

for i in range(5):
    print makePhone()

425-426-8902
473-775-2793
434-624-8356
287-630-4560
861-431-7659

Upvotes: 0

Related Questions