Home  >  Article  >  Backend Development  >  Detailed explanation and examples of Python random number random module

Detailed explanation and examples of Python random number random module

高洛峰
高洛峰Original
2017-03-28 15:03:202445browse

This article shares with you some common methods of the Python random number random module. It is very simple. If you like it, you can continue to explore it in depth

">

The random module is a module that comes with Python. In addition to generating the simplest random numbers, it also has many functions.

random.random()

is used to generate a random number. Random floating point number between 0~1, range [0,10

>>> import random
>>> random.random()
0.5038461831828231

random.uniform(a,b)

Returns a random floating point number between a and b, in the range [a, b] or [a, b), depending on rounding, a is not necessarily larger than b small.

52.98730193316595

random.randint(a,b)
Returns the
integer
between a and b, range [a,b], note: passed in The parameter must be an integer, and a must be smaller than b

>>> random.randint(50,100)

54

>>> random.randint(100,50) Traceback (most recent call last):

File
"", line 1, in <
module
>
random.randint(100,50)
File "C:\Python27\lib\random.py", line 242, in randint return self.randrange
(a, b+1)
File "C:\Python27\lib\random.py", line 218, in randrange
raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)ValueError: empty range for randrange() (100,51, -49)>>> random.randint( 50.5,100.6)

Traceback (most recent call last): File "", line 1, in random.randint(50.5,100.6)
File "C:\Python27\lib\random.py", line 242, in randint
return self.randrange(a, b+1)
File "C:\Python27\lib\random. py", line 187, in randrange
raise ValueError, "non-
integer
arg 1 for randrange()"
ValueError: non-integer arg 1 for randrange()

random.randrang([start], stop[, step])
Returns an integer within a range, and step can be set. Only integers can be passed in, random.randrange(10, 100, 2), and the result is equivalent to obtaining a random number from the sequence [10, 12, 14, 16, … 96, 98].
>>> random.randrange(100)

58

>>> random.randrange(10,100,2)

54

random .choice(sequence)


Gets an element randomly from the sequence,
list
, tuple,

string

all belong to sequence. The sequence here needs to be of ordered type. random.randrange(10,100,2) is equivalent in result to random.choice(range(10,100,2).

>>> random.choice(("stone","scissors", "paper"))'stone'>>> random.choice(["stone","scissors","paper"])'scissors'>> ;> random.choice("Random")

'm'


random.
shuffle
(x[,random])

is used to insert the list into The elements are scrambled, commonly known as shuffling.

>>> poker = ["A","2","3","4","5". ,"6","7","8","9","10","J","Q","K"]>>> random.shuffle(poker)>>> poker

['4', '10', '8', '3', 'J', '6', '2', '7', '9', 'Q ', '5', 'K', 'A']

random.sample(sequence,k)

Randomly obtain k elements from the specified sequence and return them as a fragment. The sample function will not modify the original sequence.

>>> poker = ["A","2","3","4","5","6","7","8","9" ,"10","J","Q","K"]
>>> random.sample(poker,5)
['4', '3', '10', '2', 'Q']

The above methods are some of the commonly used methods in Python, but there are many stories about random numbers. Decomposition next time~

The above is the detailed content of Detailed explanation and examples of Python random number random module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn