-
random.randint()[1,5]左闭右闭、np.random,randint()[1,5)左闭右开
Help on method randint in module random: randint(a, b) method of random.Random instance Return random integer in range [a, b], including both end points.
Help on built-in function randint: randint(...) method of mtrand.RandomState instance randint(low, high=None, size=None, dtype='l') Return random integers from `low` (inclusive) to `high` (exclusive). Return random integers from the "discrete uniform" distribution of the specified dtype in the "half-open" interval [`low`, `high`). If `high` is None (the default), then results are from [0, `low`). Parameters ---------- low : int Lowest (signed) integer to be drawn from the distribution (unless ``high=None``, in which case this parameter is one above the *highest* such integer). high : int, optional If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if ``high=None``). size : int or tuple of ints, optional Output shape. If the given shape is, e.g., ``(m, n, k)``, then ``m * n * k`` samples are drawn. Default is None, in which case a single value is returned. dtype : dtype, optional Desired dtype of the result. All dtypes are determined by their name, i.e., 'int64', 'int', etc, so byteorder is not available and a specific precision may have different C types depending on the platform. The default value is 'np.int'. .. versionadded:: 1.11.0
import random
for i in range(5):
for k in range(7):
print(random.randint(1,5),end=' ')
print()
3 4 3 4 1 1 3
1 2 1 4 1 4 3
3 2 1 2 3 3 4
1 4 1 4 4 4 5
4 1 4 3 3 4 3
import numpy as np
for i in range(5):
for k in range(7):
print(np.random.randint(1,5),end=' ')
print()
3 2 4 4 1 4 3
2 2 1 2 1 4 4
2 1 3 1 2 4 3
3 2 1 3 4 2 3
1 4 2 2 2 3 3
import numpy as np
for i in range(5):
for k in range(7):
print(np.random.randint(1,5),end=' ')
print()
1 1 1 2 2 2 2 3 3 3 2 1 4 4 3 2 1 1 2 3 4 3 2 4 3 1 4 2 4 3 1 2 2 1 3
np.random.randint(1,7,3)
array([1, 4, 6])
np.random.randint(1,7,(3,1))
array([[3],
[4],
[4]])
np.random.randint(1,7,size=(3,1))
array([[3],
[4],
[4]])
import random
print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数
print( random.random() ) # 产生 0 到 1 之间的随机浮点数
print( random.uniform(1.1,5.4) ) # 产生 1.1 到 5.4 之间的随机浮点数,区间可以不是整数
print( random.choice('tomorrow') ) # 从序列中随机选取一个元素
print( random.randrange(1,100,2) ) # 生成从1到100的间隔为2的随机整数
a=[1,3,5,6,7] # 将序列a中的元素顺序打乱
random.shuffle(a)
print(a)
9
0.030119183897754143
3.7540964998698882
t
79
[5, 6, 3, 7, 1]
print(random.sample('zyxwvutsrqponmlkjihgfedcba',5)) # 多个字符中生成指定数量的随机字符:
['d', 'c', 'p', 'm', 'n']
import string
ran_str = ' '.join(random.sample(string.ascii_letters + string.digits, 8)) # 从a-zA-Z0-9生成指定数量的随机字符:
print (ran_str)
5 b a 0 3 r f 9