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