在范围内打印随机电话号码

发布于 2025-02-03 02:27:58 字数 2876 浏览 2 评论 0原文

我正在尝试打印大量的电话号码。在这种情况下,电话号码具有格式(24)99999999。 (为了清楚起见,括号是偶然的。)我需要此列表随机分配,并具有某些约束。

前两个数字必须在1124之间 以下两个将是第三位和第四位,必须在6799之间。

这是我到目前为止所做的:

ddd = list(range(11, 24))                                                                                                                                                                     
op = list(range(67, 99))                                                                                                                                                                      
list1 = list(range(100, 999))                                                                                                                                                                 
list2 = list(range(1234, 9999))                                                                                                                                                               
for d in ddd:                                                                                                                                                                                 
    ddd = d                                                                                                                                                                                   
    #print(ddd)                                                                                                                                                                               
    for fixos in op:                           
        pre = fixos                            
        #print(pre)                            
        for l in list1:                       
            part1 = l                                                                         
            #print(part1)                                                                     
            for x in list2:   
                part2 = x                                                                    
                #print(part2)                                                                 
                #print(f"({ddd}) {pre}{l}-{l2}")                                              
                numbers = str(ddd) + str(pre) + str(l) + str(x)                              
                #print(numbers)                                                               
                requests.urllib3.disable_warnings()

list1list2构成数字的其余部分,应尽可能随机。

但是,当我运行这样的脚本时,它像这样打印

numbers : 11671001000                                                                                                                                                      
numbers : 11671001001

了一个。可悲的是,这不是我打算做的。我如何随机化它?

I'm trying to print a large list of phone numbers. In this context, phone numbers have a format (24)999999999. (Parenthesis for clarity, they are incidental.) I need this list to be randomized, with certain constraints.

The first two digits must be between 11 and 24,
the following two, which will be the the third and the fourth digits, must be between 67 and 99.

This is what I've done so far:

ddd = list(range(11, 24))                                                                                                                                                                     
op = list(range(67, 99))                                                                                                                                                                      
list1 = list(range(100, 999))                                                                                                                                                                 
list2 = list(range(1234, 9999))                                                                                                                                                               
for d in ddd:                                                                                                                                                                                 
    ddd = d                                                                                                                                                                                   
    #print(ddd)                                                                                                                                                                               
    for fixos in op:                           
        pre = fixos                            
        #print(pre)                            
        for l in list1:                       
            part1 = l                                                                         
            #print(part1)                                                                     
            for x in list2:   
                part2 = x                                                                    
                #print(part2)                                                                 
                #print(f"({ddd}) {pre}{l}-{l2}")                                              
                numbers = str(ddd) + str(pre) + str(l) + str(x)                              
                #print(numbers)                                                               
                requests.urllib3.disable_warnings()

The list1 and list2 compose the rest of the number, which should be as random as possible.

However when I run the script it prints like this

numbers : 11671001000                                                                                                                                                      
numbers : 11671001001

It goes one by one. Sadly, that's not what I intended to do. How do I get it randomized?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热风软妹 2025-02-10 02:27:58

您可以尝试此代码,它可以制作100,000个数字,您也可以将范围编辑为100,000

import random
ddd = list(range(11, 25))
op = list(range(67, 100))
for i in ddd:
    for j in op:
        beg=(str(i)+str(j))
        for p in range(100000):
            random_number=random.randint(1000000, 10000000)
            print(beg+str(random_number))

you can try this code it make 100,000 number also you can edit range 100,000 to your goal number

import random
ddd = list(range(11, 25))
op = list(range(67, 100))
for i in ddd:
    for j in op:
        beg=(str(i)+str(j))
        for p in range(100000):
            random_number=random.randint(1000000, 10000000)
            print(beg+str(random_number))
谎言月老 2025-02-10 02:27:58

我想提出一个不同的解决方案:

from random import shuffle
from itertools import cycle
from pprint import pprint
country_code = list(range(11, 24))
operator_code = list(range(67, 99))
exchange_code = list(range(100, 999))
terminal_code = list(range(1234, 9999))


target_len = 100000
ls = set()
while len(ls) < target_len:
    shuffle(country_code)
    shuffle(operator_code)
    shuffle(exchange_code)
    shuffle(terminal_code)
    numbers = zip(cycle(country_code), cycle(operator_code), cycle(exchange_code), terminal_code)
    for c, o, e, t in numbers:
        ls.add(f"{c} {o}{e}-{t}")


pprint(ls)
print(len(ls))
# To randomize final output
ls = [i for i in ls]
shuffle(ls)

由于订购了集合,因此在此处排序的输出,但它可以使用洗牌和周期对随机进行一些改进,并确保您不会使用A SET的约束重复一个数字。通常,我将其作为一种以不同方式思考随机化的方式 - 在选择替换的随机数(randint)时,是一条路线,shuffle() and code>和选择()都是确保随机性的好方法,您可以使用zip() and cycle()进行实质上创建您的随机流,从中选择。

解决这个问题的真正方法是创建一个发电机函数 - 我必须考虑如何解决这个问题,但这也是这里的解决方案。

I'd like to propose a different solution:

from random import shuffle
from itertools import cycle
from pprint import pprint
country_code = list(range(11, 24))
operator_code = list(range(67, 99))
exchange_code = list(range(100, 999))
terminal_code = list(range(1234, 9999))


target_len = 100000
ls = set()
while len(ls) < target_len:
    shuffle(country_code)
    shuffle(operator_code)
    shuffle(exchange_code)
    shuffle(terminal_code)
    numbers = zip(cycle(country_code), cycle(operator_code), cycle(exchange_code), terminal_code)
    for c, o, e, t in numbers:
        ls.add(f"{c} {o}{e}-{t}")


pprint(ls)
print(len(ls))
# To randomize final output
ls = [i for i in ls]
shuffle(ls)

The output here is ordered because sets are ordered, but it makes some improvements on randomization using shuffle and cycle, and ensures you aren't repeating a number using the constraints of a set. Mostly I offer it as a way to think about the randomization differently - while selecting a random number with replacement (randint) is a route, shuffle() and choice() are both good ways to ensure randomness, and you can do things with zip() and cycle() to essentially create randomized streams that you're choosing from.

The real way to go about this would be to create a generator function - I'd have to think a little harder how to go about that, but it's also a possible solution here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文