Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การสร้าง ID แบบสุ่มใน Python

เราเคยสร้างตัวเลขสุ่มในโปรเจกต์สำหรับข้อมูลตัวอย่าง ซึ่งนำไปใช้ทดสอบ เติมคอลัมน์ว่าง หรือวัตถุประสงค์อื่นๆ อีกมาก สิ่งสำคัญคือเราต้องสร้างข้อมูลสุ่ม ใน Python มีหลายวิธีในการสร้างข้อมูลสุ่ม และเราจะสำรวจบางวิธีที่นี่

โมดูล random() ของ Python

หนึ่งในไลบรารีสำคัญที่มาพร้อมกับ 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:

Input:

random.sample(companies, 20)

Output:

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()

ตัวสร้างเลขสุ่มแบบปลอม (Pseudo-random) ใน 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

ตามที่เห็นจากตัวอย่างข้างต้น หาก seed เหมือนกัน จะสร้างค่าก่อนหน้าแรกได้ แต่ละค่า seed ตรงกับลำดับค่าที่สร้างขึ้นคงที่สำหรับตัวสร้างเลขสุ่มที่กำหนด

สร้างเลขสุ่มที่ปลอดภัยทางเข้ารหัสใน Python

เราสามารถสร้างเลขสุ่มที่ปลอดภัยทางเข้ารหัสใน Python 3.x ได้ หากเรามี Python 3.6 หรือสูงกว่า เราสามารถใช้โมดูล secrets ใหม่และฟังก์ชัน randbelow สำหรับมัน ฟังก์ชันนี้จะสร้างเลขสุ่มต่ำกว่าค่าที่ระบุ

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'