-
给出0到10之间的随机整数:
import random
a = random.randint(0,10)
print(a)
-
给出9到10之间的随机实数:
import random
a = random.uniform(9,10)
print(a)
-
从9、19、29、39、……、99之间,随机选取一个实数:
import random
a = random.randrange(9, 100, 10)
print(a)
-
从列表[5,6,7,8,9]里面,随机选取一个数:
import random
a = random.choice([5,6,7,8,9])
print(a)
-
从一个字符串里面,随机选取一个字符:
import random
a = random.choice("从一个字符串里面,随机选取一个字符!")
print(a)
-
随机打乱列表里面的字符顺序:
import random
a = ["p","q","r","s","t","p","q","r","s","t","p","q","r","s","t",]
random.shuffle(a)
print(a)
-
从列表里面随机选取9个数字:
import random
a = range(3,100,2)
b = random.sample(a, 9)
print(b)
https://jingyan.baidu.com/article/c45ad29c038d02051753e283.html
https://blog.csdn.net/autoliuweijie/article/details/51982514
可以从一个int数字或1维array里随机选取内容,并将选取结果放入n维array中返回。
1
说明:
numpy.random.choice(a, size=None, replace=True, p=None)
a : 1-D array-like or int
If an ndarray, a random sample is generated from its elements.
If an int, the random sample is generated as if a was np.arange(n)
size : int or tuple of ints, optional
replace : boolean, optional
Whether the sample is with or without replacement
p : 1-D array-like, optional
The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.
1
2
3
4
5
6
7
8
9
10
11
示例
>>> np.random.choice(5, 3)
array([0, 3, 4])
>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])
>>> np.random.choice(5, 3, replace=False)
array([3,1,0])
>>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0])
>>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'],
https://blog.csdn.net/autoliuweijie/article/details/51982514