Usage example of the random function random in the random python

 

Article introduces the usage examples of random random function in python, detailed description and examples of usage of python random function random.

A, profile module random Python standard library random function, may generate a random floating point, integer, string, or even help you select a random element in the list in sequence, a set of data upset.

Two, random important function module 1), random () returns 0 random real numbers n <= n <between 1; 2), choice (seq) returns a random element from the sequence of seq;

import random

a = random.choice([1, 2, 3, 4])

print(a)

 

3), getrandbits (n) Returns the n random bits long integer form; 4), shuffle (seq [, random]) specified in seq situ;

4), shuffle (seq [, random]) specify the place seq.; 5), sample (seq, n) selecting n elements from random and independent sequences seq; third, random modular approach described

  1. random.random () This function module is the most common method, and it generates a random floating point number in the range between 0.0 and 1.0.
  2. random.uniform () make up the deficiencies of the above functions, it can set the range of floating-point numbers, a is the upper limit, a lower limit.
  3. the random.randint () generated randomly an integer int type, this range can be specified integer, the same upper and lower limits, python random.randint.
  4. The random.choice () may be any sequence from such list file, select a random element returns, may be used for strings, lists, and other tuples.
  5. random.shuffle () if you want a sequence of elements, random upset, then you can use this function method.
  6. random.sample () can be randomly taken from the specified segment length specified sequence, it is not modified in situ.

Fourth, the need to import the random module

1、random.random

Random character decimal random.random () for generating a 0 to 1: 0 <= n <1.0

 random.random() 

2、random.uniform

Random.uniform function prototype is: random.uniform (a, b), for generating a random point values ​​within the specified range, two parameters one of which is the upper limit, a lower limit. If a> b, the generated random number n: a <= n <= b. If a <b, then b <= n <= a.

random.uniform(1, 10)

3、random.randint

the random.randint () function prototype: random.randint (a, b), for generating an integer within a specified range. Wherein A is a lower limit of the parameter, the parameter b is the upper limit, the generated random number n: a <= n <= b

 random.randint(10, 100)

4, random.randrange

Random.randrange function prototype is: random.randrange ([start], stop [, step]),, acquires a random number from within the specified range of the specified base set increments. Such as: random.randrange (10, 100, 2), the result is equivalent to obtaining a random number from [10, 12, 14, 16, ... 96, 98] in the sequence. random.randrange (10, 100, 2) equivalent random.choice (range (10, 100, 2) on the results.

Randomly selected the even-numbered 0-100: 

import random 
 random.randrange (0, 101, 2)

5、random.choice

random.choice acquiring a random element from the sequence. Which is a function prototype: random.choice (sequence). It represents an ordered sequence parameter type. Here to explain: sequence in python is not a specific type, but generally refers to a range of types. list, tuple, strings belong sequence. You can view the relevant sequence python manual data models in this chapter.

The random.choice ( ' ABCDEFG #% ^ & * F ' ) # random character 
' D ' 

 The random.choice ([ ' Apple ' , ' PEAR ' , ' Peach ' , ' Orange ' , ' Lemon ' ]) # randomly selected string : 
' Lemon '

6、random.shuffle

Random.shuffle function prototype is: random.shuffle (x [, random]), for a list of elements disrupted. Such as:

= the p-[ " Python " , " IS " , " Powerful " , " the Simple " , " and SO ON ... " ] 
random.shuffle (the p-) 
Print the p- 
 # result (because the random, so your results may vary. ) 
# [ 'Powerful', 'Simple', 'I AM', 'the Python', 'ON and SO ...'] 
P = [ " the Python " , " IS " , " Powerful " , " Simple ", "and so on..."]

7、random.sample

Random.sample function prototype is: random.sample (sequence, k), obtaining random fragments specified length from a specified sequence. sample function does not modify the original sequence. If k is larger than the number of the sequence element, then error. 

 

 
 

 

Guess you like

Origin www.cnblogs.com/hewanli/p/11605883.html