Python uses the current time and random number to generate a unique number. python current time

Source: Internet
Author: User

Python uses the current time and random number to generate a unique number. python current time

This example describes how Python uses the current time and random number to generate a unique number. We will share this with you for your reference. The details are as follows:

The current generation time of Python is much simpler than the Java code. For details about the Java generation time, refer to the Java method for obtaining the Current System Event System. currentTimeMillis ().

The Code is as follows:

#-*-coding:utf-8-*-import datetimenow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")print now;

The running result is as follows. The current time is output. The time format is based onstrftime("%Y-%m-%d %H:%M:%S")The corresponding format is as simple as the C-language printf statement.

Python can use the following statement to generate a unique Random Number:

Import random; print random. randint (0,100); # The generated random integer n, where 0 <=n <= 100

Therefore, we can use the current time and random number to generate a unique number for uploading and generating files,

Considering the extreme situation, there are 1000 users uploading files within one second, and there will be no conflict. The best case is to use the current time + 0 ~ A random integer of 1000 to construct a random number:

The only thing worth noting is that the splicing time and the string must be converted to a string before splicing, rather than directly adding them. Because Python does not declare variables, directly adding them will become the addition of numbers.

At the same time, note that if a random number is randomly assigned to a number less than 10, a 0 value should be added before the random number, so that the number of digits generated is the same.

The Code is as follows:

#-*-Coding: UTF-8-*-import datetime; import random; nowTime = datetime. datetime. now (). strftime ("% Y % m % d % H % M % S"); # generate the current time randomNum = random. randint (0,100); # the random integer n generated, where 0 <=n <= 100if randomNum <= 10: randomNum = str (0) + str (randomNum ); uniqueNum = str (nowTime) + str (randomNum); print uniqueNum;

The following Python program uses for to set up 10 test cases to test the results generated within one second. The Code is as follows:

#-*-Coding: UTF-8-*-import datetime; import random; for I in range (): nowTime = datetime. datetime. now (). strftime ("% Y % m % d % H % M % S"); # generate the current time randomNum = random. randint (0,100); # the random integer n generated, where 0 <=n <= 100 if randomNum <= 10: randomNum = str (0) + str (randomNum ); uniqueNum = str (nowTime) + str (randomNum); print uniqueNum;

The running result is as follows:

PS: Here are several other related tools for your reference:

Tools for generating personal data randomly online:
Http://tools.jb51.net/aideddesign/rnd_userinfo

Online random character/random password generation tool:
Http://tools.jb51.net/aideddesign/rnd_password

Online random number/string generation tool:
Http://tools.jb51.net/aideddesign/suijishu

Online query of common phone numbers:
Http://tools.jb51.net/bianmin/pub_tel

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.