'Making random phone number
I want to create phone number generator with the following restrictions:
- start with 0.
- Second number must be 1.
- Third number can be on these 2,3,5.
- Number is 11 digits.
- Don't want the same number twice.
This is the code I used:
# import module import random as r
ph_no = []
ph_no2 = []
# the first number should be in the range of 6 to 9
ph_no.append(r.randint(0, 1))
ph_no2.append(r.randint(1, ))
# the for loop is used to append the other 9 numbers.
# the other 9 numbers can be in the range of 0 to 9.
for i in range(1, 10):
ph_no.append(r.randint(0, 9))
ph_no2.append(r.randint(0,9))
# printing the number
for i in ph_no:
print(i, end="")
This is the problem:
ph_no2.append(r.randint(1, ))
TypeError: Random.randint() missing 1 required positional argument:
'b'
I want help to make this code work.
Edite:
I used this code to print the output into a txt file but it doesn't print all the output:
with open("textfile.txt", "w") as external_file:
add_text = "This text will be added to the file"
print(pn(), file=external_file)
external_file.close()
Solution 1:[1]
randint has an argument for start and stop. You're missing one
# 0 is start, 10 is stop
>>> r.randint(0,10)
6
Use ph_no2.append(r.randint(start, stop))
Here's a phone number generator with your requirements:
import random as r
def pn():
# start with 0.
# Second number must be 1.
number = '01'
# Third number can be on these 2,3,5.
number += r.choice('235')
# Number is 11 digits.
for i in range(8):
number += r.choice('0123456789')
return number
>>> pn()
'01523693965'
Edit:
To get n numbers, and none are the same
# Set does not have duplicates
all_numbers = set()
n = 10
while len(all_numbers) < n:
# Continue adding until you have `n` numbers
x = pn()
print(x)
all_numbers.add(x)
01561915353
01558729923
01512093981
01589320526
...
Solution 2:[2]
This uses random's random.randint(a, b) and random.choice(seq):
#!/usr/bin/env python
import random
def random_number():
number = list(
str(random.randint(11111111111, 99999999999))
) # 11 random digits to str
number[0] = '0' # start with 0
number[1] = '1' # second number must be 1
number[2] = str(random.choice([2, 3, 5])) # third number can be 2, 3, or 5
return ''.join(number)
Usage: random_number() returns a random number
for i in range(0, 9):
print(random_number())
Output example:
01247829529
01389527108
01513481001
01284462585
01305116767
01219669837
01239182461
01245292538
01284163077
Explanation: Works by casting int -> str -> list -> editing the individual numbers in place -> rejoining to a single str -> int()
Solution 3:[3]
import random as r
for x in range(10):
ph_no = [0,1] # first 2 numbers
ph_no.append(*r.sample([2,3,5],1)) # Third number
#remaining numbers
while len(ph_no)<11:
k=r.randint(0, 9)
if k not in ph_no[1:]: # we are skipping zero as we cannot have 11 unique numbers from 0-9
ph_no.append(k)
else:
continue
print(''.join(str(x) for x in ph_no))
#output
01327486095
01508746239
01567293084
01346857920
01520693874
01369405827
01384290657
01563082479
01520947386
01532460879
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 |
