Python module | random module

 

random () method returns a real number randomly generated.

 

1. random.random () 0 to 1 random float.

print(random.random())          # 0.7056803343481585

 

2. random.uniform (a, b) within a specified range for generating random point values, two parameters one of which is the upper limit, a lower limit. Usage scenarios: red envelopes

print(random.uniform(1,3))        # 1.4427911142943668

 

() Random integer 3. random.randint. Be used: PIN

Print ((l, 5) the random.randint)          # integer between 1-54    

 

Random.randrange 4. ([Start], STOP [, STEP])   , increasing the specified base set of obtaining a random number from within the specified range. Note: randrange care regardless of the end

Print (random.randrange (1, 10,))     # between odd 1-10 3

 

5. random.choice () a random selection. Usage scenarios : sweepstakes, codes

random.choice([1,'23',[4,5]])

 

6. random.sample () randomly selecting a plurality of return, the number returned is a function of the second parameter . Usage scenarios: pumping more than winning

random.sample ([. 1, ' 23 ' , [4,5]], 2)       # list elements in any combination of two [[4, 5], '23']

The value is the probability of the same, such as the number 3, then the probability of each number is 1/3. sample, you can do the draw. For example, the company year-end awards, 10 third prize, direct extraction 10 on it.

 

7. random.shuffle () disrupt the order of the list. Usage scenarios: shuffle

= Item [1,3,5,7,9 ] 
random.shuffle (Item)             # disarranged order 

Print (Item)                          # [. 9,. 3,. 7,. 5,. 1] # changed 
Print (random.shuffle (Item) )          # None 
Print (Item)                          # [. 1,. 9,. 5,. 7,. 3] # changed again

Guess you like

Origin www.cnblogs.com/Summer-skr--blog/p/12124029.html