"Python" 13-bit random serial number generation tool Source analysis

Source: Internet
Author: User



by Dolphin, beijing,20150712



0x00 background



Recently in the language of learning Python, just learned for loop, for many sentence syntax is not familiar. Just today, see there is a site activity, you need to enter a 13-bit serial number to determine whether you win, but this 13-bit serial number is required to buy their home products to obtain, it costs a certain amount of money, so I was thinking, is not able to write a serial number generator to take a chance, So decided to use the new Python beginner's knowledge to write.



0x01 Knowledge Point Preparation



The main function of this tool is to generate random letters to make serial numbers, and the random () function in Python is to randomly generate a string or a number. Here is a brief description of the functions used:



Random.randint (A, B)



randomly generates a random number N of a range within a <= n <= B, such as:


import random
a = random.randint(2,5)
print(a)


Random.randrange (a)



The function is the same as the previous function, except that only one parameter is required, and the randomly generated number range is 0 <= N <= A, starting from zero and ending at A-1.


import random
a = random.randrange(5)
print(a)


random.choice (seq)



This function function is to randomly select a string from the string sequence seq and returnindexerrorIf the sequence is empty.


seq = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘
a   = random.choice(seq)
print(a)


0X02 Functional Requirements



Through the analysis of known 13-bit serial number, found that there are rules to follow, the serial number can basically be divided into two kinds:



1, the serial number is divided into three parts, the first part is divided into 4 random letters, the 5th and 6th digits are random numbers, the latter 7 are random letters;



2, this serial number is basically no rule, there is only one rule, that is, 13 random serial number, only 2 digits are random numbers, the rest are randomly generated letters;



0X03 Source



Since just learned python not long, so only the first case of the code, the second case of interest in children's shoes can be coded or simplified, such as the following code is wrong or can be simplified place, welcome to point out!


# Random generate some character string
# By Dolphin, 20150712
import random

# Generate random string Part One
stra = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’ #define the range of randomly generated strings
strb = ‘‘
i = 4;
while i> 0: # loop that generates the first 4 random strings of the first part
strb + = random.choice (stra)
i-= 1
print ("Part One:", strb)

#Generate random string Part Two
stra2 = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’
strb2 = ‘‘
i2 = 7
while i2> 0: # loop that generates a random string of 7 bits after the second part
strb2 + = random.choice (stra2)
i2-= 1
print ("Part Two:", strb2)

# Generate random number
num1 = str (random.randrange (9)) #Generate a random number in the middle
num2 = str (random.randrange (9))
s = strb + num1 + num2 + strb2 #combine a randomly generated three-part string
print (s)


0X04 Extended Application



After practicing with the above examples, you can extend this by saving randomly generated strings in txt text, or by generating a "dictionary" of 13-bit strings, or by writing a dictionary generation tool that is more complex.



If you just want to generate a string of 13-bit random strings, without the two qualifying terms I said, there is a more concise code that can be implemented as follows:





from random import Randomdef random_str(randomlength=8):    str = ‘‘    chars = ‘AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789‘    length = len(chars) - 1    random = Random()    for i in range(randomlength):        str+=chars[random.randint(0, length)]    return str




-------------------------------------------------------------------------------------



Resources:



1. "The Python standard Library",



Https://docs.python.org/3/library/random.html#module-random



2, "Random module in Pyhton", Capricorn.python,



Http://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html



3, "Using Python to generate a fixed-length random string",Huwei,



http://www.oschina.net/code/snippet_153443_4752






This article is from the "Siberian Wolf 026" blog, make sure to keep this source http://1429223.blog.51cto.com/1419223/1673590



"Python" 13-bit random serial number generation tool Source analysis


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.