posted on 2023-05-21 18:04 read(973) comment(0) like(23) collect(3)
Table of contents
1. random.random(): returns a floating-point number randomly generated in the range [0,1)
2. random.uniform(a, b): returns a randomly generated floating-point number in the range [a, b)
3. random.randint(a,b): generate an integer within the specified range
5. random.choice(): Get a random element from the specified sequence
6. random.shuffle(x[,random]): Used to shuffle the elements in a list and sort them randomly
13. random.seed(): set random seed
First we need to import the random module
- import random
- print(random.random())
- import random
- print(random.uniform(1,5))
- import random
- print(random.randint(1,10))
For example random.randrange(10,100,2) , the result is equivalent to getting a random number from the sequence [10,12,14,16...96,98]. random.randrange ( 10,100,2 ) is equivalent to random.choice(range(10,100,2)) in result.
- import random
- print(random.randrange(10,22,3))
random.choice() obtains a random element from the sequence, its prototype is random.choice(sequence) , and the parameter sequence represents an ordered type. Let me explain here that sequence is not a specific type in Python, but generally refers to a sequence data structure . Lists, tuples, and strings all belong to sequence .
- import random
- print(random.choice('学习python')) # 从字符串中随机取一个字符
- print(random.choice(['good', 'hello', 'is', 'hi', 'boy'])) # 从list列表中随机取
- print(random.choice(('str', 'tuple', 'list'))) # 从tuple元组中随机取
- import random
- p=['hehe','xixi','heihei','haha','zhizhi','lala','momo..da']
- random.shuffle(p)
- print(p)
- x = [1, 2, 3, 4, 5]
- random.shuffle(x)
- print(x)
- import random
- list1=[1,2,3,4,5,6,7,8,9,10]
- slice=random.sample(list1,5)
- print(slice)
- #[8, 3, 5, 9, 10]
- print(list1)
- #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- x = random.sample(range(0, 10), 5)
- print(x, type(x))
- #[9, 2, 7, 8, 6] <class 'list'>
- Words = "AppleKMedoide"
- print(random.sample(Words, 3))
- #['p', 'M', 'A']
- print(random.sample(Words, 3))
- #['d', 'i', 'l']
The following functions need to call the numpy library
- import random
- import numpy as np
- x = np.random.rand()
- y = np.random.rand(4)
- print(x,type(x))
- #0.09842641570445387 <class 'float'>
- print(y,type(y))
- #[0.27298291 0.12350038 0.63977128 0.90791234] <class 'numpy.ndarray'>
np.random.normal(loc=a, scale=b, size=()) - returns the probability density random number of the normal distribution (Gaussian distribution) satisfying the condition of mean=a, standard deviation =b, size defaults to None (returns 1 random number), can also be int or array
- import random
- import numpy as np
- x = np.random.normal(10,0.2,2)
- print(x,type(x))
- #[9.78391585 9.83981096] <class 'numpy.ndarray'>
- y = np.random.normal(10,0.2)
- print(y,type(y))
- #9.871187751372984 <class 'float'>
- z = np.random.normal(0,0.1,(2,3))
- print(z,type(z))
- #[[-0.07114831 -0.10258022 -0.12686863]
- # [-0.08988384 -0.00647591 0.06990716]] <class 'numpy.ndarray'>
- z = np.random.normal(0,0.1,[2,2])
- print(z,type(z))
- #[[ 0.07178268 -0.00226728]
- # [ 0.06585013 -0.04385656]] <class 'numpy.ndarray'>
np.random.randn(d0, d1, ... dn): Returns a probability density random number from a standard normal distribution (mean=0, standard deviation=1) ,
- import random
- import numpy as np
- x = np.random.randn()
- y = np.random.randn(3)
- z = np.random.randn(3, 3)
- print(x, type(x))
- print(y, type(y))
- print(z, type(z))
np.random.standard_normal(): returns the probability density random number of the standard normal distribution (mean=0, standard deviation=1), size defaults to None (returns 1 random number), and can also be an int or an array
- import random
- import numpy as np
- x = np.random.standard_normal()
- y = np.random.standard_normal(size=(3,3))
- print(x, type(x))
- print(y, type(y))
The results of np.random.rand() and np.random.standard_normal() are similar , both of which return random floating-point numbers or arrays that conform to the standard normal distribution.
np.random.randint(a, b, sizie=(), dytpe=int) - size defaults to None (returns 1 random number), and can also be int or array
- import random
- import numpy as np
- # 从序列[0, 10)之间返回shape=(5,5)的10个随机整数(包含重复值)
- x = np.random.randint(0, 10, size=(5, 5))
- # 从序列[15, 20)之间返回1个随机整数(size默认为None, 则返回1个随机整数)
- y = np.random.randint(15, 20)
- print(x, type(x))
- print(y, type(y))
After setting the random seed to 10, the random number of random.random() will be directly set to: 0.5714025946899135
- import random
- random.seed(10)
- x = random.random()
- print(x,type(x))
- random.seed(10)
- y = random.random()
- print(y,type(y))
- z = random.random()
- print(z,type(z))
The random number is generated like this: We regard this complex algorithm (called a random number generator) as a black box, throw the seed we prepared into it, and it will return you two things, one is The random number you want, the other is a new seed that is guaranteed to generate the next random number, put the new seed into the black box, and get a new random number and a new seed, and then generate random numbers The road is getting farther and farther.
We use the following code to test:
- import numpy as np
- if __name__ == '__main__':
- i = 0
- while i < 6:
- if i < 3:
- np.random.seed(0)
- print(np.random.randn(1, 5))
- else:
- print(np.random.randn(1, 5))
- i += 1
- i = 0
- while i < 2:
- print(np.random.randn(1, 5))
- i += 1
- print(np.random.randn(2, 5))
- np.random.seed(0)
- print("###################################")
- i = 0
- while i < 8:
- print(np.random.randn(1,5))
- i += 1
Through this experiment we can get the following conclusions:
This article comprehensively refers to the following articles:
The usage of random in pythonhttps://blog.csdn.net/shoushou_/article/details/119652905?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165352582116781685333907%2522%252C%2522scm% 2522%253A%252220140713.130102334..%2522%257D&request_id =165352582116781685333907&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-2-119652905-null-null.142^v10^pc_search_result_control_group,157 ^v12^control&utm_term=python+random&spm=1018.2226.3001.4187python random function_PandaDou's blog-CSDN blog_python random functionhttps://blog.csdn.net/m0_37822685/article/details/80363530?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-803635 30-blog- 119652905.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-80363530-blog-119652905.pc_relevant_paycolumn_v3&utm _relevant_index=1Summary of commonly used random random functions in python, detailed usage and differences between functions--a picture to understand python random functions_Ruo Zhilan's Blog-CSDN Blog_python Random Functionhttps://blog.csdn.net/weixin_45914452/article/details/115264053?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1 -115264053-blog- 110164241.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-115264053-blog-110164241.pc_relevant_default&utm_relev ant_index=1Understanding of Random Seedshttps://blog.csdn.net/qq_31511955/article/details/110424334
Author:evilangel
link:http://www.pythonblackhole.com/blog/article/25332/2773e642f3fbdf272a80/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!