1.random
def random(self):
"""Get the next random number in the range [0.0, 1.0)."""
return (int.from_bytes(_urandom(7), 'big') >> 3) * RECIP_BPF
翻译:获取0,1之间的随机浮点数
1 #!/usr/bin/python 2 import random 3 print(random.random())#返回随机生成的一个浮点数,范围在[0,1]之间View Code
2.uniform
def uniform(self, a, b):
"Get a random number in the range [a, b) or [a, b] depending on rounding."
return a + (b - a) * self.random()
翻译:获取在[a,b]之间的随机浮点数
1 #!/usr/bin/python 2 import random 3 print(random.uniform(1,2))#返回随机生成的一个浮点数,范围在[a,b]之间View Code
3.randint
def randint(self, a, b):
"""Return random integer in range [a, b], including both end points.
"""
return self.randrange(a, b+1)
翻译:返回[a,b]之间的整数,两边都包含
1 #!/usr/bin/python 2 import random 3 print(random.randint(1,9))#返回随机生成的一个整数,范围在[a,b]之间View Code
4.randrange
def randrange(self, start, stop=None, step=1):
"""Choose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
"""
翻译:返回一个随机目标在范围range(star,stop,step) 中,
则修复了在randint()中包含最后一位的问题,在python中不是你常见的
1 #!/usr/bin/python 2 import random 3 print(random.randrange(1,10,2))#返回随机生成的一个整数,范围在[a,b]之间,步长为2的随机数View Code
5.shuffle
def shuffle(self, x, random=None):
"""Shuffle list x in place, and return None.
Optional argument random is a 0-argument function returning a
random float in [0.0, 1.0); if it is the default None, the
standard random.random will be used.
"""
翻译:打乱列表的顺序,返回None
可选参数random是0函数返回在[0.0,1.0]之间的浮点数,如果是默认None,则使用标准random,random
1 #!/usr/bin/python 2 import random 3 x=[1,2,3,4,5] 4 random.shuffle(x)#用于将列表中元素打乱 5 print(x)View Code
6.sample
def sample(self, population, k, *, counts=None):
"""Chooses k unique random elements from a population sequence or set.
Returns a new list containing elements from the population while
leaving the original population unchanged. The resulting list is
in selection order so that all sub-slices will also be valid random
samples. This allows raffle winners (the sample) to be partitioned
into grand prize and second place winners (the subslices).
Members of the population need not be hashable or unique. If the
population contains repeats, then each occurrence is a possible
selection in the sample.
Repeated elements can be specified one at a time or with the optional
counts parameter. For example:
sample(['red', 'blue'], counts=[4, 2], k=5)
is equivalent to:
sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)
To choose a sample from a range of integers, use range() for the
population argument. This is especially fast and space efficient
for sampling from a large population:
sample(range(10000000), 60)
"""
翻译:从总序列或集合中选择k个唯一或随机元素,返回一个新的列表包含从总列表来的元素,原始的未改变。
结果的列表是按选择顺序如此那么所有子集是有效的随机数。
列表中的成员不需要可hash或唯一,如果总列表包含重复数据,那么每个发生的可选的都在sample里面,
重复元素可以表指定一个或一次用参数counts比如sample(['red','blue'],counts=[4,2],k=5)
相当于sample(['red','red','red','red','blue','blue'],k=5)
1 #!/usr/bin/python 2 import random 3 #!/usr/bin/python 4 import random 5 print(random.sample(x,3))#在指定序列中获取指定长度的值,不改变原有序列View Code
7.seed
def seed(self, *args, **kwds):
"Stub method. Not used for a system random number generator."
return None
翻译:种子,标准的方法,对一个系统随机数生成器来说没用
在定义相同种子数时,返回的随机数一致,但是只用于seed调用下一行随机代码
1 #!/usr/bin/python 2 import random 3 def random_test(): 4 random.seed(3) # 设置随机数一致的情况数 5 for i in range(3): 6 print(random.random()) 7 random_test() 8 9 def random_test1(): 10 random.seed(3) # 设置随机数一致的情况数 11 for i in range(3): 12 print(random.random()) 13 random_test1()View Code
8.choice
def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
# raises IndexError if seq is empty
return seq[self._randbelow(len(seq))]
翻译:选择从不为空的序列中选择随机元素,如果序列为空,则抛出异常IndexError
1 #!/usr/bin/python 2 import random 3 print(random.choice([1,2,3]))View Code
9.choices
def choices(self, population, weights=None, *, cum_weights=None, k=1):
"""Return a k sized list of population elements chosen with replacement.
If the relative weights or cumulative weights are not specified,
the selections are made with equal probability.
"""
翻译:在所有元素中选择k个列表来替代
如果相关权重没指定,则返回机率都一样
1 #!/usr/bin/python 2 import random 3 def random_test(): 4 for i in range(3): 5 print(random.choices([2,3,4,5],weights=[1,1,2,3],k=3)) 6 7 random_test()View Code 标签:常用,python,self,random,sample,range,def From: https://www.cnblogs.com/Little-Girl/p/17989803