标签:style blog http io color os ar for sp
random是用于生成随机数的,我们可以利用它随机生成数字或者选择字符串。
|
1
2
|
import randomrandom.random() #输出 0.5487876445645461 |
|
1
2
3
4
|
import randomrandom.uniform(10,20) #输出 15.999997038152358random.uniform(20,10) #输出 12.718038067741021random.uniform(10,10) #输出 10.0 |
|
1
2
3
4
|
import randomrandom.randint(10,20) #输出 12random.randint(10,10) #输出 10random.randint(20,10) #Error |
|
1
2
3
4
|
import randomrandom.randrange(10,100) #输出为10到100间的任意数random.randrange(10,100,4) #输出为10到100内以4递增的序列[10,14,18,22...]random.choice(range(10,100,4)) #输出在结果上与上一条等效 |
|
1
2
3
4
5
6
7
8
9
|
import randomrandom.choice(range(10)) #输出0到10内随机整数random.choice(range(10,100,2)) #输出随机值[10,12,14,16...]random.choice("I love python") #输出随机字符I,o,v,p,y...random.choice(("I love python")) #同上random.choice(["I love python"]) #输出“I love python”random.choice("I","love","python") #Errorrandom.choice(("I","love","python")) #输出随机字符串“I”,“love”,“python”random.choice(["I","love","python"]) #输出随机字符串“I”,“love”,“python” |
|
1
2
3
4
|
import randomlist=[‘I‘,‘love‘,‘python‘,‘very‘,‘much‘]random.shuffle(list)print list #输出乱序list |
|
1
2
3
4
5
6
7
8
9
10
|
import randoma=‘123456789‘b=[1,2,3,4,5,6,7,8,9]c=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]random.sample(a,3)random.sample(b,3)random.sample(c,3) #随机取三个元素最为一个片段返回[6,4,3]print aprint bprint c #a,b,c值不变 |
标签:style blog http io color os ar for sp
原文地址:http://www.cnblogs.com/wipy/p/4064719.html