Home > other >  Python random module
Python random module

Time:09-16

A full explanation of the Python random,
First of all, we want to know, the random module we are commonly used, randint, random, choice, choices, uniform, shuffle, randrange.
These we can all help way to actually find usage, in an interactive interface with help (method) will tell, for example:
Import the random
> Help (random. Randint)
Help on method randint module in the random:

Randint (a, b) method of the random. The random instance
Return the random integer in the range [a, b], o both end points.
The above is the content of the output,
We will introduce these usage,
Randint:
Randint (a, b) in a to b at random an integer equal to the probability, including the endpoint
The random:
The random () in [0, 1) random out a decimal
Choice:
Choice (a list), and then randomly select a number from the list,
For example the random choice ([1, 2, 3]) after the operation will appear random 1, 2, 3
Choices:
Choices (a list of probability, XXX, k=number), here is more complex when such calls, he would use probability k times select elements from the list:
Eg: choices ([1, 2, 3], [0.1, 0.2, 0.7], k=5) will be in 1 corresponds to 0.1, 2, 3, 0.2, 3 corresponding probability of 0.7 appeared five Numbers
Note: the third parameter choices here I don't understand, so finally it with k=5, with 5 directly complains the default value is 1 k
Uniform:
Uniform (a.) in a, b in a normal distribution probability between several
Shuffle:
Shuffle (a list), as the English word, shuffling, upset the list order
Eg: shuffle ([1, 2, 3]) after the operation will be may disrupt the order of 1, 2, 3
Randrange:
Randrange (start, end=None, they count=1), there is two is the default, you can change, if not the end, you can choose the Numbers from 0 to start,
If there is a end, from start to finish, one step is step such as if you start from 1 kay, count to 2 so he will be from 3, 5, 7 and other digital selective
Eg: randrange,7,2 (1) after the operation will appear 3, 5, 7

  • Related