Python uses the current time, random number to produce a unique number of methods to explain

Source: Internet
Author: User
This article mainly describes how Python uses the current time, random numbers to produce a unique number of methods, involving Python time and random numbers related operations skills, the need for friends can refer to the following

This example describes how Python produces a unique number using the current time and random number. Share to everyone for your reference, as follows:

Python generates the current time is simple, much shorter than Java code, Java generation time can refer to the Java Get current system event System.currenttimemillis () method

The specific code is as follows:


#-*-coding:utf-8-*-import Datetimenow = Datetime.datetime.now (). Strftime ("%y-%m-%d%h:%m:%s") print now;

The result of the operation is as follows, the output current time, the time format according to strftime("%Y-%m-%d %H:%M:%S") , the corresponding format, just as simple as the C language printf statement.

Python can generate a unique random number using the following statement:


Import Random;print random.randint (0,100); #生成的随机整数n, where 0<=n<=100

Based on this, we can use the current time, random number to produce a unique number, for some upload files, produce files and other occasions,

Considering the extreme situation, in 1 seconds there are 1000 users uploading files, there is no conflict between the best case, is to use the current time +0~1000 random integer, constructs a random number:

The only thing to note is that splicing time and string are converted into strings instead of being added directly, and since Python is not a variable declaration, the direct addition becomes the sum of the numbers.

Also note that if a random number is randomly numbered to a number less than 10, it should be preceded by a 0 of the randomly generated number so that the number of digits generated is the same.

The specific code is as follows:


#-*-coding:utf-8-*-import datetime;import Random;nowtime=datetime.datetime.now (). Strftime ("%Y%m%d%H%M%S"); Generates the current time Randomnum=random.randint (0,100); #生成的随机整数n, where 0<=n<=100if randomnum<=10:  randomnum=str (0) +str ( Randomnum); Uniquenum=str (Nowtime) +str (randomnum);p rint uniquenum;

The following Python program will use the for setting 10 test cases to test the results produced in a second, with the following code:


#-*-coding:utf-8-*-import Datetime;import random;for i in Range (0,10):  Nowtime=datetime.datetime.now (). strftime ("%y%m%d%h%m%s"); #生成当前时间  Randomnum=random.randint (0,100); #生成的随机整数n, where 0<=n<=100  if randomnum<= Ten:    randomnum=str (0) +str (randomnum);  Uniquenum=str (nowtime) +str (randomnum);  Print Uniquenum;

The resulting running results, such as, can occur without one of the numbers being the same:

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.