random - generating a pseudo-random number, the module for the distribution of a variety of pseudo-random number generator. Let's look at a few common modules introduce random function:
First, the method for integer:
random.randrange (GFC)
random.randrange (start, stop [, step
]) is equivalent to the choice (range (start, stop, step)), but does not actually create an object range.
>>> Import Random >>> Print (random.randrange (. 4)) # returns a random integer of 0 to 3 1 >>> Print (random.randrange (1,. 9)) # returns a random integer of 1 to 8 8 > >> Print (random.randrange (1,. 11, 2)) # returns a random odd number of 1 to 10 3
random.randint (a, b)
returns a random integer N, a <= N <= b, is equivalent to randrange (a, b + 1) .
>>> Print (the random.randint (1, 10)) # Returns [1, 10] is a random integer . 8 >>> Print (the random.randint (-10, -1)) # Returns [-10, -1] random integer, can not be written the random.randint (-1, -10) -5
Second, a method for the sequence:
The random.choice (SEQ)
returns a random element from a non-empty sequence, if the sequence is empty, the trigger IndexError.
>>> Print (The random.choice ( ' BHasdgiHUI1234 ' )) # returns a character the I >>> Print (The random.choice ([ ' Apple ' , ' Peach ' , ' PEAR ' ])) # Returns a list of a random element PEAR >>> Print (the random.choice (( ' Bob ' , ' Jhon ' , ' of Micheal ' ))) # tuple returns a random element Jhon
random.choices (population, weight = None,
*, cum_weight = None, k = 1) random k elements taken from population alternative sequence composition list, the list is returned. weight is relative weights sequence, cum_weight cumulative weight, while developing and cumulative weight weight weight cum_weight, will produce a type error.
>>> print(random.choices(['red', 'yellow','green', 'blue', 'black', 'pink', 'purple', 'white'], k=4)) ['red', 'red', 'purple', 'black'] >>> print(random.choices(['apple', 'peach', 'pear'], [1, 2, 3], k=4)) ['pear', 'pear', 'pear', 'peach'] >>> print(random.choices(['apple', 'peach', 'pear'], [6, 12, 2], k=4)) ['peach', 'peach', 'pear', 'pear']
random.shuffle (x [, random])
rearranged sequence (shuffle)
= SEQ >>> [ ' red1 ' , ' yellow2 ' , ' Blue3 ' , ' green4 ' ] >>> random.shuffle (SEQ) # list reordering >>> Print (SEQ) [ ' red1 ' , ' Blue3 ' , ' green4 ' , ' yellow2 ' ]
random.sample (population, k)
returns a random number K from the elements will not be repeated in a sequence population consisting of the new queue. Irreplaceable for random sampling. It returns a new list does not destroy the original list. If the new queue length k greater than the length of the original population of the queue, an error is thrown ValueError.
>>> Print (random.sample ([ ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' . 1 ' , ' 2 ' , ' . 3 ' ], 2 )) # random list extracting two elements of the new list of [ ' D ' , ' B ' ] >>> Print ( '' .join (random.sample ( "ahoiNUAI483*4"4 ))) UohI
Third, the true value distribution:
random.random ()
returns a random floating point numbers, the range [0, 1)
>>> print(random.random()) 0.5867129667371662
random.uniform (a, b)
returns a random floating point between a and b, if a> b, the floating-point between b and a is returned. a and b are likely to appear in the results.
>>> print(random.uniform(1, 10)) 5.961863773592117 >>> print(random.uniform(2,6)) 3.2723769810535543