Python之random模块详解
python的random模块
random模块是python中一个生成随机数的模块。
random不是python解释器内置的模块。
导入random模块的方法是:
<pre class="brush:php;toolbar:false">import random</pre>如果只使用random模块中的单个方法的话,也可以使用
<pre class="brush:php;toolbar:false">from random import method_name</pre>例如:
我只想生成一个10以内的随机的整数,不需要random模块的别的方法的时候,也可以使用以下命令
<pre class="brush:php;toolbar:false">from random import randint random.randint(0,10)</pre>查看random模块的内置方法可以使用以下命令:
<pre class="brush:php;toolbar:false">dir(random)</pre>其中常用的方法有下面几个:
choice
<pre class="brush:php;toolbar:false">#从一个非空列表中随机选择一个元素 >Choose a random element from a non-empty sequence.</pre><pre class="brush:php;toolbar:false">>>> random.choice([1,3,5,7]) 1 >>> random.choice([1,3,5,7]) 3</pre>相关推荐:《Python视频教程》
randint
<pre class="brush:php;toolbar:false">#从a和b(包括b)的范围内随机生成一个整数 >Return random integer in range [a, b], including both end points.</pre><pre class="brush:php;toolbar:false">>>> random.randint(0,9) 8 >>> random.randint(0,9) 0 >>> random.randint(0,9) 4 >>> random.randint(0,9) 3</pre>random
<pre class="brush:php;toolbar:false">#生成一个0(包括0)到1内的浮点数 >random() -> x in the interval [0, 1).</pre><pre class="brush:php;toolbar:false">>>> random.random() 0.3898009217264272 >>> random.random() 0.897328889551127 >>> random.random() 0.9899842422616898</pre>randrange
<pre class="brush:php;toolbar:false">#在指定范围内随机生成一个整数 > Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want.</pre><pre class="brush:php;toolbar:false">>>> random.randrange(100,200) 156 >>> random.randrange(100,200) 133 >>> random.randrange(10,20) 11 >>> random.randrange(10,20) 15</pre>sample
<pre class="brush:php;toolbar:false">#从一个列表或集合中随机选择多个元素 >Chooses k unique random elements from a population sequence or set.</pre><pre class="brush:php;toolbar:false">>>> random.sample([23,[1,2,3],"aa","yy"],2) ['aa', 23] >>> random.sample([23,[1,2,3],"aa","yy"],3) ['aa', [1, 2, 3], 23]</pre>shuffle
<pre class="brush:php;toolbar:false">#把一个列表内元素的顺序打乱,列表的内存地址不变 >Shuffle list x in place, and return None.</pre><pre class="brush:php;toolbar:false">>>> l1=[1,"a",3,5,"b","c"] >>> id(l1) 140436582171208 >>> random.shuffle(l1) >>> print(l1) [1, 'b', 'a', 'c', 3, 5] >>> id(l1) 140436582171208</pre>uniform
<pre class="brush:php;toolbar:false"> #在指定范围内随机生成一个浮点数 >Get a random number in the range [a, b) or [a, b] depending on rounding.</pre><pre class="brush:php;toolbar:false">>>> random.uniform(12,33) 27.02416276339153 >>> random.uniform(12,33) 13.832414985007832 >>> random.uniform(12,33) 12.827493699496461</pre>现在想生成一个5位包含大小写和数字的随机验证码,代码如下:
<pre class="brush:php;toolbar:false">import random def random_code(): random_str = "" for i in range(5): #随机选择一个整数 num=random.randint(0,9) #生成一个大写字母 upper=chr(random.randint(65,90)) #生成一个小写字母 lower=chr(random.randint(97,122)) #每次从大小写字母中随机选择一位 res=random.choice([str(num),upper,lower]) random_str =res return random_str print(random_code())</pre>运行5次这个程序,生成的验证码如下:
<pre class="brush:php;toolbar:false">KwlTN t1Pag 294l6 t1Pag 294l6</pre>您可能感兴趣的文章:
Python之random模块详解
一文带你读懂Python中的模块
python怎么导入random
怎么用python导入随机库?
Python random模块及用法
python中如何随机分配
python如何生成随机数
python标准库有哪些
python随机数模块怎么导入
如何实现python随机生成数字?