Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods of random module in Python

Shulou Source: shulou.com Published: 2022-06-03 12:11:04 11月15日 Update

Xiaobian to share with you what are the methods of random module in Python, I hope you have gained something after reading this article, let's discuss it together!

Random generates random decimals between 0 and 1.

randrange produces a number within the first two parameters.

Excluding the second parameter, the third parameter is the step size.

randint produces a number between two parameters, both left and right parameters can be obtained.

choice Randomly selects an element from a list and returns it.

Shuffle Disorders the order of elements in a list.

examples

import random r = random.random() #Get a random fraction between 0 and 1 print(r) r = random. random range (1,15,2) #Print a random number between 1 and 15(excluding 15), step size of 2 can only print (1,3,5,7,9,11,13)print(r) r = random. randomint (1 ~ 10) #Generate a random number from 1 to 10(including 10) print(r) list1 =<$'Tom ','Jack','Lily']r = random.choice(list1) #choice puts a list, randomly prints an element in the list print(r) pai =<$'Heart K','Ace of Diamonds',' Five of Clubs','Six of Spade'] random.shuffle(pai) #Shuffle list elements print(pai) #Captcha upper and lower case letters and numbers def func(): code = '' for i in range(4): ran1 = str(random.randint(0,9)) ran2 = chr(random.randint(65,90)) ran3 = chr(random.randint(97,122)) r = random.choice([ran1,ran2,ran3]) code +=r return coder = func()print(r) After reading this article, I believe you have a certain understanding of "what are the methods of random module in Python". If you want to know more about it, please pay attention to the industry information channel. Thank you for reading!

Tags: Parameters elements two between numbers methods modules decimals articles random numbers order step size three actions size letters done examples more clubs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn OPPO Reno Linux Shulou Tech Info