Python's built-in module random stochastic module approach and use case (implementation of five-digit random verification code)

Source: Internet
Author: User
Tags shuffle


1. Random (self):


Get the next random number in the range [0.0, 1.0)



Take 0 to 1 random floating-point numbers directly


 
 
import random
print (random.random ())

C: \ python35 \ python3.exe D: / pyproject / day21 module / random random module.py

0.3105503800442595
2, Randint (self, A, b)


Return random integer in range [A, b], including both end points.



Returns a random integer between a A and a, including A and b


import random
print (random.randint (5,99))

C: \ python35 \ python3.exe D: / pyproject / day21 module / random random module.py

53 
3, Randrange (self, start, stop=None, Step=1, _int=int):


Choose a random item from range (Start, stop[, step]).
This fixes the problem with Randint () which includes the
Endpoint In Python this is usually don't what want



Returns a direct random integer, not including B, that is >=a smaller than the range of B


import random
print (random.randrange (1,9))

C: \ python35 \ python3.exe D: / pyproject / day21 module / random random module.py

3 


You can also specify the step size


import random
print (random.randrange (1,9, step = 2))

C: \ python35 \ python3.exe D: / pyproject / day21 module / random random module.py

5 
4, Choice (self, seq)


Choose a random element from a non-empty sequence [? EL?M?NT] Element sequence sequence



Take a random element inside a sequence that is not empty


import random
print (random.choice ([11,22,33]))

C: \ python35 \ python3.exe D: / pyproject / day21 module / random random module.py

twenty two 
5. Sample (self, population, K)


Chooses k unique random elements from a population sequence or set



Select K random elements from the sequence inside or within the collection, to return a list



Here's an example of randomly fetching 2 elements from a collection.


import random
print (random.sample ({11,22, "gouguoqi", 66}, 2))

C: \ python35 \ python3.exe D: / pyproject / day21 module / random random module.py

[‘Gouguoqi’, 11] 
6. Uniform (self, A, b):


Get a random number in the range [A, b] or [A, b] depending on rounding



Select a floating-point number of random numbers between A and b


import random
print (random.uniform (2,900))

C: \ python35 \ python3.exe D: / pyproject / day21 module / random random module.py

621.520221600369 
7, Shuffle (self, x, random=None)
is to shuffle the elements in the list (in a scrambled order, with no eggs)
import random
ret = [11,22,33,44,55]
random.shuffle (ret)
print (ret)

C: \ python35 \ python3.exe D: / pyproject / day21 module / random random module.py

[33, 22, 55, 44, 11] 


8, the production of five-digit random verification code


import random
def v_code ():
     ret = ""
     for n in range (5): # loop several times
         num = random.randint (0,9) #take a random number between 0-9
         alf = chr (random.randint (65,122)) #chr between 65 and 122 is the range of lowercase a to z and uppercase A to Z
         s = str (random.choice ([num, alf])) # choose a random element from the list and convert it to str
         ret + = s # Add s to ret each time, s is a string, so the last ret is a 5 digit and letter combination verification code
     return ret
print (v_code ())

C: \ python35 \ python3.exe D: / pyproject / day21 module / random random module.py

6s070 


Python's built-in module random stochastic module approach and use case (implementation of five-digit random verification code)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.