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