[英]Is there any way to generate unique numbers in python?
Is there a way to generate unique numbers in python?有没有办法在 python 中生成唯一编号? Sort of like a database primary key?
有点像数据库主键?
And the random
module of python can sometimes generate the same number.而python的
random
模块有时可以生成相同的数字。 I want it to be completely unique.我希望它完全独一无二。
import uuid
uuid.uuid4()
# UUID('e052c11d-e8ae-4a3d-a3e8-10d428a2e935')
UUID is a solution to generate generic random and unique values used as id. UUID 是一种生成用作 id 的通用随机值和唯一值的解决方案。 If you want in integer random and unique, you need a database or another data management system.
如果要在 integer 中随机且唯一,则需要数据库或其他数据管理系统。
You can use time and then hash the output可以使用时间再hash output
import time
now = time.time()
print(now)
print(hash(now))
the output is: output 是:
1586515688.2441766
563032968135724776
with this way you have unique number forever.这样你就永远拥有唯一的号码。
import numpy as np
# create a (5, 5) array containing unique integers drawn from [0, 100]
uarray = np.random.choice(np.arange(0, 101), replace=False, size=(5, 5))
# check that each item occurs only once
print((np.bincount(uarray.ravel()) == 1).all())
# True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.