在 Python 中生成随机 ID


我们在项目中使用随机数生成样本数据,这些数据稍后可用于测试、填充空列或用于许多其他目的,关键是我们需要生成随机数据。在 Python 中,有许多方法可以生成随机数据,我们将在本文中探讨其中的一些方法。

Python random() 模块

Python 自带的一个重要库是 random,我们将在代码中始终使用它。

要在代码中使用此模块,您只需导入它,就是这样,我们就可以使用它了。

import random

让我们看看如何使用它:

 在线演示

import random
print("What i will get, no idea as i'm using random.random()")
print(random.random())

输出

What i will get, no idea as i'm using random.random()
0.5306626723173611

如果我第二次尝试运行相同的程序,您将获得不同的输出:

What i will get, no idea as i'm using random.random()
0.5504289430397661

关于 random 模块的一些要点

  • random() 是 random 模块的基本函数
  • random 模块几乎所有函数都使用 random() 函数。
  • Random() 函数将生成 [0.0 到 1.0) 之间的任意数字。

在 Python 中生成随机整数

下面我们使用两个函数来生成随机整数:

  • randint()
  • randrange()

 在线演示

from random import randint, randrange
print("Printing random integer ", randint(0, 20))
print("Printing random integer ", randrange(0, 20, 2))

输出

Printing random integer 15
Printing random integer 4

从列表中随机选择一个项目

假设我们有一个公司名称列表,我们想从该列表中检索一个项目(公司名称)。我们可以通过以下方式实现:

 在线演示

import random
companies = ['RELIANCE', 'TCS', 'INFY', 'SBI', 'PNB','HDFC']
print('Randomly selecting company from a list: ', random.choice(companies))

输出

Randomly selecting company from a list: INFY

从列表中随机选择多个项目

考虑上面的例子,但我们想从列表中随机选择多个项目(公司),而不是一个项目(公司),我们可以通过 random.sample() 函数实现:

 在线演示

import random
companies = ['RELIANCE', 'TCS', 'INFY', 'SBI', 'PNB','HDFC']
print('Randomly selecting 3 companies from a list: ', random.sample(companies,3))

输出

Randomly selecting 3 companies from a list: ['TCS', 'RELIANCE', 'INFY']

但是,如果我们尝试选择的项目数量超过列表中的项目数量,我们会遇到 ValueError:

输入:

random.sample(companies,20)

输出:

ValueError: Sample larger than population or is negative

从列表中选择多个随机项目的另一种方法是 – random.choices()。

 在线演示

import random
companies = ['RELIANCE', 'TCS', 'INFY', 'SBI', 'PNB','HDFC']
print('Randomly selecting 3 companies from a list: ', random.choices(companies,k=6))

输出

Randomly selecting 3 companies from a list: ['TCS', 'TCS', 'INFY', 'HDFC', 'INFY', 'TCS']

从上面的输出可以看出,使用 random.choices() 方法可能会从列表中获得重复的项目。

Python 中的伪随机数生成器

伪随机数生成器通过对某个值执行某些操作来工作。通常,此值是生成器先前生成的数字。但是,第一次使用生成器时,没有先前的值。

 在线演示

import random
print("Seed value 10: ") # Initialize seed value
random.seed(10)
for i in range(5):
print(random.randint(1,100))
print()
print("Seed Value 5: ") # this time we'll get different values
random.seed(5)
for i in range(5):
print(random.randint(1,100))
print()
print("Seed value: 10") # will get the same result, what we got initially
random.seed(10)
for i in range(5):
print(random.randint(1,100))

输出

Seed value 10:
74
5
55
62
74
Seed Value 5:
80
33
95
46
89
Seed value: 10
74
5
55
62
74

从上面的例子可以看出,如果种子相同,它会生成第一个先前的值。对于给定的随机数生成器,每个种子值对应于一个固定的生成值序列。

在 Python 中生成密码学安全的随机数

我们可以在 Python 3.x 中生成密码学安全的随机数。如果我们使用 Python 3.6 或更高版本,我们可以使用新的 secrets 模块和下面的 rand 函数。它将在指定的值以下生成一个随机数。

 在线演示

import secrets
#generate 10 secure random numbers between 10 and 500
for x in range(0,10):
secV =10+ secrets.randbelow(500)
print(secV)

输出

464
406
184
293
399
332
495
292
118
134

对于 Python 3.5 或更低版本,另一种方法是使用 random 模块和 SystemRandom 类来生成密码学安全的随机数,方法是:

 在线演示

import random
randGen = random.SystemRandom()
for x in range(0,10):
secV = 10+ randGen.randint(0,499)
print(secV)

输出

374
211
425
264
217
97
210
39
319
52

另一种方法是使用 random 和 secrets(用于保护数据)模块。

 在线演示

import secrets
import random
secNum = random.SystemRandom().random()
print("secure number is ", secNum)
print("Secure byte token", secrets.token_bytes(16))

输出

secure number is 0.5205307353786663
Secure byte token b'\x05T>\xacsqn0\x08\xc4\xf4\x8aU\x13\x9f\xcf'

更新于: 2020-06-30

2K+ 次查看

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告
© . All rights reserved.