本文目录导读:

Python版本(推荐)
import random
import string
def generate_random_string(length=8):
"""生成指定长度的随机字符串"""
characters = string.ascii_letters + string.digits
return ''.join(random.choice(characters) for _ in range(length))
def batch_generate(count=10, length=8, prefix='', suffix=''):
"""
批量生成随机字符串
:param count: 生成数量
:param length: 字符串长度
:param prefix: 前缀
:param suffix: 后缀
"""
strings = []
for _ in range(count):
random_str = generate_random_string(length)
full_str = prefix + random_str + suffix
strings.append(full_str)
return strings
# 使用示例
if __name__ == "__main__":
# 生成10个长度为8的随机字符串
result = batch_generate(count=10, length=8)
for i, s in enumerate(result, 1):
print(f"{i}: {s}")
print("\n--- 带前缀后缀的示例 ---")
# 生成带前缀后缀的随机字符串
result2 = batch_generate(count=5, length=6, prefix="TEST_", suffix="_2024")
for i, s in enumerate(result2, 1):
print(f"{i}: {s}")
高级功能版本:
import random
import string
import argparse
import sys
def generate_random_string(length=8, use_letters=True, use_digits=True, use_punctuation=False):
"""生成随机字符串,可自定义字符集"""
chars = ''
if use_letters:
chars += string.ascii_letters
if use_digits:
chars += string.digits
if use_punctuation:
chars += string.punctuation
if not chars:
chars = string.ascii_letters + string.digits
return ''.join(random.choice(chars) for _ in range(length))
def main():
parser = argparse.ArgumentParser(description='批量生成随机字符串')
parser.add_argument('-c', '--count', type=int, default=10, help='生成数量 (默认: 10)')
parser.add_argument('-l', '--length', type=int, default=8, help='字符串长度 (默认: 8)')
parser.add_argument('-p', '--prefix', default='', help='前缀')
parser.add_argument('-s', '--suffix', default='', help='后缀')
parser.add_argument('--no-letters', action='store_true', help='不使用字母')
parser.add_argument('--no-digits', action='store_true', help='不使用数字')
parser.add_argument('--punctuation', action='store_true', help='使用标点符号')
parser.add_argument('-o', '--output', help='输出到文件')
args = parser.parse_args()
strings = []
for _ in range(args.count):
random_str = generate_random_string(
args.length,
use_letters=not args.no_letters,
use_digits=not args.no_digits,
use_punctuation=args.punctuation
)
strings.append(args.prefix + random_str + args.suffix)
if args.output:
with open(args.output, 'w') as f:
f.write('\n'.join(strings))
print(f"已写入 {args.count} 个字符串到 {args.output}")
else:
for i, s in enumerate(strings, 1):
print(f"{i}: {s}")
if __name__ == "__main__":
main()
Linux/Unix Shell版本
#!/bin/bash
# 随机字符串生成函数
generate_random() {
local length=${1:-8}
# 使用urandom生成随机字符串
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w "$length" | head -n 1
}
# 批量生成
batch_generate() {
local count=${1:-10}
local length=${2:-8}
local prefix=${3:-""}
local suffix=${4:-""}
for ((i=1; i<=count; i++)); do
random_str=$(generate_random "$length")
echo "${prefix}${random_str}${suffix}"
done
}
# 使用示例
echo "=== 生成10个8位随机字符串 ==="
batch_generate 10 8
echo -e "\n=== 带前缀后缀 ==="
batch_generate 5 6 "PREFIX_" "_2024"
更多shell版本:
#!/bin/bash
# 方法1:使用openssl
generate_ssl() {
local length=${1:-8}
openssl rand -base64 48 | tr -dc 'a-zA-Z0-9' | cut -c1-"$length"
}
# 方法2:使用/dev/urandom和head
generate_urandom() {
local length=${1:-8}
head /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c "$length"
echo
}
# 方法3:结合date和随机数
generate_date_random() {
local length=${1:-8}
local rand_str=$(date +%s%N | md5sum | head -c "$length")
echo "$rand_str"
}
# 主函数
main() {
local count=${1:-10}
local length=${2:-8}
local method=${3:-"urandom"} # urandom, ssl, date
echo "生成 $count 个长度为 $length 的随机字符串 (方法: $method)"
for ((i=1; i<=count; i++)); do
case $method in
ssl) generate_ssl "$length" ;;
date) generate_date_random "$length" ;;
*) generate_urandom "$length" ;;
esac
done
}
# 运行
main "$@"
Windows Batch版本
@echo off
chcp 65001 >nul
echo 批量生成随机字符串
echo =====================
set /a count=10
set length=8
set prefix=
set suffix=
REM 生成随机字符串(简单实现)
for /l %%i in (1,1,%count%) do (
set "str="
setlocal enabledelayedexpansion
for /l %%j in (1,1,%length%) do (
set /a "rand=!random! %% 62"
if !rand! lss 10 (
set "char=!rand!"
) else if !rand! lss 36 (
set /a "char=!rand! + 87"
set "char=!char!"
for /f %%c in ('cmd /u /c echo !char! ^| find /v ""') do set "char=%%c"
) else (
set /a "char=!rand! + 29"
for /f %%c in ('cmd /u /c echo !char! ^| find /v ""') do set "char=%%c"
)
set "str=!str!!char!"
)
echo !str!
endlocal
)
echo.
echo 完成!
pause
PowerShell版本
# 批量生成随机字符串函数
function New-RandomStrings {
param(
[int]$Count = 10,
[int]$Length = 8,
[string]$Prefix = "",
[string]$Suffix = "",
[switch]$IncludeSpecialChars
)
$charSet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
if ($IncludeSpecialChars) {
$charSet += '!@#$%^&*()_+-=[]{}|;:,.<>?'
}
$results = @()
for ($i = 0; $i -lt $Count; $i++) {
$randomStr = -join ((1..$Length) | ForEach {
$charSet[(Get-Random -Maximum $charSet.Length)]
})
$results += "$Prefix$randomStr$Suffix"
}
return $results
}
# 使用示例
$randomStrings = New-RandomStrings -Count 10 -Length 8
$randomStrings | ForEach-Object { Write-Host $_ }
# 带前缀后缀的示例
$customStrings = New-RandomStrings -Count 5 -Length 6 -Prefix "USER_" -Suffix "_2024"
$customStrings | ForEach-Object { Write-Host $_ }
使用方法
Python版本
# 基本使用 python random_strings.py # 高级使用 python random_strings.py -c 20 -l 12 -p "ID_" -s "_2024" -o output.txt
Shell版本
# 赋予执行权限 chmod +x random_strings.sh # 运行 ./random_strings.sh # 或指定参数 ./random_strings.sh 20 12 urandom
根据需要选择适合的版本,Python版本功能最完善,Shell版本最适合Linux环境使用。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播