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

How to use the random number Random in python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use the random number Random in python". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to use the random number Random in python" can help you solve the problem.

I. brief introduction of random module

The random function in the Python standard library can generate random floating-point numbers, integers, strings, and even help you randomly select an element in the list sequence, disrupt a set of data, etc., that is, used to generate random numbers.

2. Random module important function (1) random.random () function

The random.random () function, which generates a random floating-point number in the range of 0.01mm 1.0.

> import random > print (random.random ()) 0.2203627723039484 > print (random.random ()) 0.124968990086466 > print (random.random ()) 0.4156471793210381

Note: you can't write random.Random () like this.

> print (random.Random ()) (2) random.randint () function

Random.randint () function prototype is: random.randint (a, b), randomly generate an integer int type, you can specify the range of this integer, used to generate a specified range of integers. Parameter an is the lower limit and parameter b is the upper limit. The random number generated is n: a > random.randint (121023) 12 > random.randint (12mem23) 19 > random.randint (12L12) / / the random value is invariant 12 > random.randint (12L12) 12 > random.randint (- 8jur12) 3 > random.randint (- 8L12)-6 > random.randint (- 8L12)-2 (3) random.uniform () function.

The prototype of the random.uniform () function is random.uniform (a, b), which is used to generate a number of random character points in a specified range, one of which is the upper limit and the other is the lower limit.

> random.uniform (2.1pyr2.8) 2.571350659171257 > random.uniform (21th) 28.09088226941614 > > random.uniform (48,921) / / it is not recommended to write this way. Generally speaking, the value of the first parameter is smaller than that of the second parameter 46.42492852213456 > random.uniform (- 4pen21) 7.81666451991304 > random.uniform (- 4Jing 21) 6.382703447293181 > random.uniform (- 4.22.8)-0.4436054791279309 > random.uniform (- 4.22.8) 2.7511228545721043 (4) random.randrange (function)

The prototype of the random.randrange () function is random.randrange ([start], stop [, step]), which gets a random number from a set that is incremented by a specified cardinality within a specified range. Start: start value, stop: end value, step1: step size, default is 1, you can leave it unwritten

> random.randrange > > random.randrange > 40 > random.randrange 55 > random.randrange (06pint 5) 15 > random.randrange (- 8Power6)-2 > random.randrange (- 8pc6) 4 > random.randrange (- 8mr6) / / step length defaults to 1. At this time, you don't have to write 5 > > random.randrange (- 8pen6) (5) random.choice () function.

The prototype of the random.choice () function is: random.choice (sequence), which can be returned from any sequence, such as a list list, and can be used for strings, lists, tuples, and so on. The parameter sequence represents an ordered type. Sequence is not a specific type in python, but refers to a series of types in general. List, tuple, and strings all belong to sequence.

> random.choice ("12045ouhuhjn") / / string'u' > random.choice ("12045ouhuhjn")'4' > random.choice (1mem5pies 4, "j", 4)) / / tuple 1 > random.choice (1pint 5) 4, "j", 4)) 5 > random.choice ([1mr4, 8pr, 6p0p3]) / list 4 > random.choice ([1mr4, 8pas, 6jpg3]) (6) random.shuffle () function

The prototype of the random.shuffle () function is random.shuffle (x [, random]), which is used to scramble elements in a list.

> p = ["Python", "is", "powerful", "simple", "and so on..."] > random.shuffle (p) > print (p) ['powerful',' Python', 'simple',' is', 'and so on...'] > lst = [1MJ 2MJ 5MJ 7] > random.shuffle (lst) > print (lst1) [4Med 7,5,2,1] (7) random.sample () function

The prototype of the random.sample () function is random.sample (sequence, k), which randomly gets fragments of the specified length from the specified sequence and returns the list without in-situ modification. An error will be reported if k is greater than the number of sequence elements.

> lst = [2pr 3rec 5je 7je 9je 4e] > random.sample (lst,5) [5,9,7,8,6] > tup = [1, "f", 8, "8f", 0pr 7J 5je 8] > random.sample (tup,4) [7,'8f, 'fame, 8] > str1 = "i love you 1314" > random.sample (str1,4) [' lumped, 'estranged,' o'' This is the end of the introduction to "how to use random numbers Random in python". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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