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 common functions of random library in Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you the "Python random library what common functions", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn about the "Python random library which commonly used functions" this article.

An introduction to random Library

In python, the random library is used to generate random numbers. But the random number generated by random library is a pseudo-random number generated by Mason rotation algorithm, not a real random number, because the computer can not generate real random numbers.

The random library is the standard library of python, so as long as you install the python environment, you can basically use the random library, as follows

Import random two basic random functions

The random number in the random library is generated by two basic random functions, one is the seed () function, and the other is the random () function, as follows

1. Seed ()

Seed (a=None): random number seed function, the default parameter an equals the current system time. If the parameter a value is fixed, the resulting random number will be the same value. As follows

Import randomimport timerandom.seed (axi1) ran_num1 = random.random () ran_num3 = random.randint (1,6) print ("I am the first random result:") print (ran_num1) print (ran_num3) time.sleep (3) print ("I am the second random result:") print (ran_num1) print (ran_num3)

It can be seen from the results that when the value of parameter an is determined, it is not a random number.

2. Random ()

Random (): generate a random decimal between [0.0,1.0)

A = 0while a

< 10: num = random.random() a += 1 print(num) 三 扩展随机函数 由于基本随机函数不够用,所以又有了以下的扩展随机函数,如下。 1. randint() randint(a, b): 生成一个[a, b]之间的随机整数 #randinta = 0while a < 5: num = random.randint(1, 5) a += 1 print(num)

2. Uniform ()

Uniform (a, b): generate a random decimal between [a, b]

# uniforma = 0while a < 5: num = random.uniform (1,7) a + = 1 print (num)

3. Randrange ()

Randrange (mfocus n [, k]): generate a random integer with k (default is 1) as the step size between [m < n].

# randrangea = 0while a < 5: num = random.randrange (1,5) a + = 1 print (num)

4. Choice ()

Choice (seq): randomly selects a number from a sequence

# choicea = 0while a < 5: num = random.choice ('abcd') a + = 1 print (num)

5. Shuffle ()

Shuffle (seq): scrambles the order of list elements and returns the disturbed list

# shufflel = [1,2,3,4] a = 0while a < 5: random.shuffle (l) a + = 1 print (l)

6. Sample ()

Sample (seq, n): returns n elements randomly from the specified sequence seq and returns them as a list

# samples = 'abcdfg'a = 0while a < 5: num = random.sample (s, 1) a + = 1 print (num)

Four instances (dynamic amount value)

Requirement: return a dynamic amount value, amount range:

One decimal place and two decimal places of 1.0 to 1

two。 An integer not exceeding 100

3.1 to 100 one decimal place, two decimal places; (test the amount field, whether the two decimal places will be automatically completed)

Realize

# Test the amount field, will it automatically complete two decimal places # realize the amount field dynamic value import randomdef get_random_amount (): 'realize the amount field dynamic value: return: random_amount''# one decimal place less than 1 Two decimal values amount1 = round (random.random (), 1) amount2 = round (random.random (), 2) # integer amount3 = random.randint (1,100) # one decimal place of 100 Two decimal values amount4 = round (random.uniform (1,100), 1) amount5 = round (random.uniform (1,100), 2) # randomly returns one of the five amount values randomList = [amount1, amount2, amount3, amount4, amount5] random_amount = random.sample (randomList, 1) [0] print (randomList) return random_amount# function call random_amount = get_random_amount () print (random_amount)

The test results are as follows

These are all the contents of the article "what are the common functions of the random library in Python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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