Python common modules-random number module

Source: Internet
Author: User

Python common modules-random number module

Yun Zhengjie

Copyright Notice: Original works, declined reprint! Otherwise, the legal liability will be investigated.

I. Examples of common methods

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 ImportRandom8 Print(Random.random ())#value (0,1) float type. 9 Ten Print(Random.randint (1,3))#the int type that takes the value [1,3].  One  A Print(Random.randrange (1,3))#the int type of the value [1,3].  -  -names = ["Yinzhengjie","Yun Zhengjie","Yzj","Beijing","Xian"] the Print(Random.choice (names))#randomly takes a number from the given list.  -  - Print(Random.sample (names,3))#randomly takes 3 elements from a given list.  -  + Print(Random.uniform (1,3))#the float type of the value (1,3).  -  +Num_list = [1,2,3,4,5] ARandom.shuffle (Num_list)#to scramble the order of the list at Print(num_list) -  -  -  -  - #The result of the above code execution is as follows: in0.31234235002894484 -3 to2 + Yzj -['Beijing','Yun Zhengjie','Xian'] the1.3141763537753142 *[1, 3, 5, 2, 4]

Two. Verification Code case

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 ImportRandom8 9 defValdatecode (number=5):Tenres ="" One      forIinchRange (number): Anum = Random.randint (1, 9) -string = Chr (Random.randint (97,122)) -s =Random.choice ([str (num), string]) theRes + =s -     returnRes -  -res = Valdatecode (10) + Print(RES) -  +  A  at #The result of the above code execution is as follows: -43jh12l2i5

Python common modules-random number module

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.