Python's Random module

Source: Internet
Author: User

I. INTRODUCTION

The Ramdom module provides a function of random numbers: random () It can return a randomly generated real number, within the range of [0,1]. It is important to note that the random () is not directly accessible and needs to be imported into the module to be used by random.

Two. Use
Import random# first random number print ("Random ():", Random.random ()) # Output: Random ():  0.09690599908884856# The second random number print ("Random (): ", Random.random ()) # Output: Random ():  0.8732120512570916# Random Output 1 to 2 integer containing 2print (random.randint) # output: 1 or Random output of a number, range within 1-10 print (Random.randrange (1,10))

Randomly generate code for 5-bit uppercase and lowercase letters and numbers

Import random li = []for i in range (6): ""     randomly generates 6-digit code "" "     r = Random.randrange (0, 5)     if r = = 4 or r = = 2:  # If the random number is 2 or 4 generates the number         temp = random.randrange (0)  # generates a random number         li.append (str (temp))  # int cannot use the Join method of list to convert from str to string     else:  # Otherwise randomly generated letter         temp = Random.randrange  The number corresponding to the ASCII code number corresponds to the character         C = chr (temp)         li.append (c)  result = "". Join (LI)  # join joins all elements of a list into a single string, Requires all elements to be string print (result)

  

Python's Random module

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.