用Python编写的20个随机_Feature()生成器
发布时间:2023-12-11 05:56:36
以下是用Python编写的20个随机_Feature()生成器带使用例子:
1. 随机整数生成器:
import random
def random_int():
while True:
yield random.randint(1, 100)
# 使用例子
rand_int = random_int()
print(next(rand_int)) # 输出随机整数
2. 随机小数生成器:
import random
def random_float():
while True:
yield random.uniform(0, 1)
# 使用例子
rand_float = random_float()
print(next(rand_float)) # 输出随机小数
3. 随机字母生成器:
import random
import string
def random_letter():
while True:
yield random.choice(string.ascii_letters)
# 使用例子
rand_letter = random_letter()
print(next(rand_letter)) # 输出随机字母
4. 随机姓名生成器:
import random
import string
def random_name():
first_names = ['John', 'Jane', 'Michael', 'Emma']
last_names = ['Smith', 'Doe', 'Johnson', 'Miller']
while True:
yield f"{random.choice(first_names)} {random.choice(last_names)}"
# 使用例子
rand_name = random_name()
print(next(rand_name)) # 输出随机姓名
5. 随机日期生成器:
import random
import datetime
def random_date(start_date, end_date):
while True:
yield start_date + datetime.timedelta(days=random.randint(0, (end_date - start_date).days))
# 使用例子
start_date = datetime.datetime(2020, 1, 1)
end_date = datetime.datetime(2020, 12, 31)
rand_date = random_date(start_date, end_date)
print(next(rand_date)) # 输出随机日期
6. 随机手机号码生成器:
import random
import string
def random_phone_number():
while True:
number = ''.join(random.choices(string.digits, k=10))
yield f"({number[:3]}) {number[3:6]}-{number[6:]}"
# 使用例子
rand_phone = random_phone_number()
print(next(rand_phone)) # 输出随机手机号码
7. 随机邮件地址生成器:
import random
import string
def random_email():
domains = ['gmail.com', 'yahoo.com', 'hotmail.com']
while True:
name = ''.join(random.choices(string.ascii_letters, k=10))
domain = random.choice(domains)
yield f"{name}@{domain}"
# 使用例子
rand_email = random_email()
print(next(rand_email)) # 输出随机邮件地址
8. 随机密码生成器:
import random
import string
def random_password(length=8):
while True:
password = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=length))
yield password
# 使用例子
rand_password = random_password()
print(next(rand_password)) # 输出随机密码
9. 随机颜色生成器:
import random
def random_color():
while True:
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
yield f"#{r:02x}{g:02x}{b:02x}"
# 使用例子
rand_color = random_color()
print(next(rand_color)) # 输出随机颜色值
10. 随机坐标生成器:
import random
def random_coordinates():
while True:
x = random.uniform(-90, 90)
y = random.uniform(-180, 180)
yield (x, y)
# 使用例子
rand_coords = random_coordinates()
print(next(rand_coords)) # 输出随机坐标
11. 随机网址生成器:
import random
import string
def random_website():
while True:
subdomain = ''.join(random.choices(string.ascii_letters, k=5))
domain = ''.join(random.choices(string.ascii_lowercase, k=5))
extension = random.choice(['com', 'net', 'org'])
yield f"http://{subdomain}.{domain}.{extension}"
# 使用例子
rand_website = random_website()
print(next(rand_website)) # 输出随机网址
12. 随机IP地址生成器:
import random
def random_ip_address():
while True:
parts = [str(random.randint(0, 255)) for _ in range(4)]
yield '.'.join(parts)
# 使用例子
rand_ip = random_ip_address()
print(next(rand_ip)) # 输出随机IP地址
13. 随机布尔值生成器:
import random
def random_boolean():
while True:
yield random.choice([True, False])
# 使用例子
rand_boolean = random_boolean()
print(next(rand_boolean)) # 输出随机布尔值
14. 随机文件名生成器:
import random
import string
def random_filename(length=10):
while True:
filename = ''.join(random.choices(string.ascii_letters + string.digits, k=length))
yield f"{filename}.txt"
# 使用例子
rand_filename = random_filename()
print(next(rand_filename)) # 输出随机文件名
15. 随机信用卡号生成器:
import random
def random_credit_card():
while True:
number = ''.join(random.choices(string.digits, k=16))
yield ' '.join([number[i:i+4] for i in range(0, len(number), 4)])
# 使用例子
rand_credit_card = random_credit_card()
print(next(rand_credit_card)) # 输出随机信用卡号
16. 随机价格生成器:
import random
def random_price():
while True:
yield round(random.uniform(1, 1000), 2)
# 使用例子
rand_price = random_price()
print(next(rand_price)) # 输出随机价格
17. 随机货币生成器:
import random
def random_currency():
currencies = ['USD', 'EUR', 'GBP']
while True:
yield random.choice(currencies)
# 使用例子
rand_currency = random_currency()
print(next(rand_currency)) # 输出随机货币
18. 随机邮政编码生成器:
import random
def random_postal_code():
while True:
code = ''.join(random.choices(string.digits, k=5))
yield f"{code[:3]} {code[3:]}"
# 使用例子
rand_postal_code = random_postal_code()
print(next(rand_postal_code)) # 输出随机邮政编码
19. 随机短信验证码生成器:
import random
def random_verification_code(length=4):
while True:
code = ''.join(random.choices(string.digits, k=length))
yield code
# 使用例子
rand_code = random_verification_code()
print(next(rand_code)) # 输出随机短信验证码
20. 随机UUID生成器:
import uuid
def random_uuid():
while True:
yield str(uuid.uuid4())
# 使用例子
rand_uuid = random_uuid()
print(next(rand_uuid)) # 输出随机UUID
这些生成器可以用于模拟生成各种随机数据,如测试数据、样本数据等。可以根据需要选择相应的生成器,并使用next()函数获取每次生成的随机值。
