简体   繁体   English

如何生成4-6个唯一数字

[英]How to generate 4-6 unique numbers

I am trying to generate 4-6 unique numbers in python but when I do uuid.uuuid4() , it generates something like 23dfsFe823FKKS023e343431 . 我试图在python中生成4-6个唯一数字,但是当我执行uuid.uuuid4() ,它会生成类似23dfsFe823FKKS023e343431 Is there a way I can achieve this, generate 4-6 unique numbers like 19391 or 193201. 有没有一种方法可以实现,生成4-6个唯一数字,例如19391或193201。

NB: Beginner with python 注意:Python初学者

Try this: 尝试这个:

import random

nums = set()
while len(nums) < 4: # change 4 to appropriate number
    nums.add(random.randint(0, 1000000))

For example: 例如:

>>> nums
set([10928, 906930, 617690, 786206])

yes, to make life easy lets use an easy example. 是的,让生活变得轻松,让我们举一个简单的例子。

#lets import random, to generate random stuff
import random
#create a result string
result = ''
nums = [1,2,3,4,5,6,7,8,9,0]
for i in range(6):
    result += str(random.choice(nums))

print(result)

UUID is for generating Universally Unique Identifiers, which have a particular structure and won't be what you're after. UUID用于生成通用唯一标识符,该标识符具有特定的结构,不会成为您所追求的。

You can use the random module as follows 您可以按以下方式使用random模块

import random
id = ''.join(str(random.randint(0,10)) for x in range(6))
print(id)

What does this do? 这是做什么的?

  • randint generates a random number between 0 inclusive and 10 exclusive, ie 0-9 randint生成一个0到10的随机数,即0-9
  • calling this with for x in range(6) generates six random digits for x in range(6)调用它会生成六个随机数字
  • str converts the digits to strings str将数字转换为字符串
  • ''.join forms a single string from the digits ''.join由数字组成一个字符串

if you want to use uuid to generate number then you can code something like this 如果您想使用uuid生成数字,则可以编写如下代码

digit = 5   # digits in number 

import uuid
for i in range(6):
  print(uuid.uuid4().int[:digit])

Or 要么

from uuid import uuid4
result = [ uuid4().int[:5] for  i in range(6) ]

You can use random from standard python library 您可以使用标准python库中的random

random.randint(a, b) random.randint(a,b)
Return a random integer N such that a <= N <= b. 返回一个随机整数N,使得a <= N <= b。

https://docs.python.org/3/library/random.html#random.randint https://docs.python.org/3/library/random.html#random.randint

In [1]: from random import randint                                                             

In [2]: randint(1_000, 999_999)                                                                
Out[2]: 587848

In [3]: randint(1_000, 999_999)                                                                
Out[3]: 316441

To generate N number of a random number or token by specifying a length of Token this will be an easy and feasible solution. 通过指定令牌的长度来生成N个随机数或令牌,这将是一种简单可行的解决方案。

import random

#num[]: by the combination of this, a random number or token will be generated.
nums = [1,2,3,4,5,6,7,8,9,0] 

#all the tokens or random number will be stored in a set called tokens.
#here, we are using a set, because the set will never contain duplicate values.
tokens = set()

result='' #to store temporaray values

while len(tokens) < 10000: # change 10000 to the appropriate number to define the numbers of the random numbers you want to generate.
    #change 6 to appropiate number to defined the length of the random number.  
    for i in range(6):
        result+=str(random.choice(nums))
        tokens.add(result)
        result=''


print(tokens)
#print(len(tokens))
#print(type(tokens))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM