Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of random module in Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report