randomly generated characters python

The first method: Unicode code

In unicode code, the range of characters (0x4E00, 9FBF)

This method is relatively simple, but there is a small problem, unicode code contains more than 20,000 Chinese characters, contains many rare traditional characters.

The second method: GBK2312

gbk2312 encoded using two bytes character combination, the range of the first byte is 0xB0-0xF7, range of the second byte is 0xA1-0xFE.
for encoding GBK2312 detailed explanation see GBK2312 encoding

GBK2312 a collection of more than 6,000 commonly used Chinese characters. Choice of two ways to look at needs.

import random

def Unicode():
    val = random.randint(0x4e00, 0x9fbf)
    return chr(val)

def GBK2312():
    head = random.randint(0xb0, 0xf7)
    body = random.randint(0xa1, 0xfe)
    val = f'{head:x} {body:x}'
    str = bytes.fromhex(val).decode('gb2312')
    return str

if __name__ == '__main__':
    print(Unicode())
    print(GBK2312())

A third method: a list read

# encoding: utf-8
import random

first_name = [ " king " , " Li " , " Zhang " , " Liu " , " Zhao " , " Jiang " , " Mon " , " Chen " , " Xu " , " Yang " , " Shen " , " horse " , " high " ,"", "Shangguan " , " bell " , " often " ]
SECOND_NAME = [ " Wei " , " China " , " founding " , " ocean " , " Just " , " Miles " , " loving " , " animal husbandry " , " Lu " , " Road " , " Xin " , " Xin " , " soldiers " ,"", "Zhihong " , " peak " , " Lei " , " Ray " , " text " , " Minghao " , " light " , " ultra " , " army " , " up " ]
name = random.choice(first_name) + random.choice(second_name)

print(name)

 

Guess you like

Origin www.cnblogs.com/xioawu-blog/p/12400582.html