Construction random is a common function in the program, Python built-in support for this, concise and efficient. This blog mainly records some of the functions commonly used in random.
random.random()
: Returns a floating-point number from 0 to one that is left closed right.
Return the next random floating point number in the range [0.0, 1.0].
random.uniform(a, b)
: Returns a floating-point number between A and B.
Return a random floating point number n Such this a <= N <= b for a <= B and b <= N <= A for b < A.
Implementation mode: A + (b-a) * random ()
random.randint(a, b)
: Returns an integer between A and B, including A and B.
Return a random integer n such that a <= N <= B. Alias for Randrange (A, b+1).
random.choice(seq)
: Returns a random element in the sequence seq and an error if the sequence is empty.
Return a random element from the non-empty sequence seq. If seq is empty, raises indexerror.
random.randrange(stop) ,random.randrange(start, stop[, step])
: Equivalent to Choice (range (start, stop, Step)). However, the Range object is not created.
Return a randomly selected element from range (start, stop, step). This is equivalent to choice (range (start, stop, step)), but doesn ' t actually build a Range object.
random.sample(population, k)
: Returns a list of the first k elements in the population.
Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.
random.shuffle(x[, random])
: There is no return value in itself, and the action is to disrupt the elements in X.
Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); By default, the function random ().
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python Random Module