1、hashlib hash加密
import hashlib
md5 = hashlib.md5()
md5.update("MisHou is a beautiful girl")
print md5.digest()
print md5.hexdigest()
print md5.block_size
print md5.digest_size
print '-' * 25, '-' * 25
print hashlib.new('md5', "MisHou is a beautiful girl").hexdigest()
print hashlib.new('sha1', "MisHou is a beautiful girl").hexdigest()
2、random随机数生成
random.random()用于生成一个0到1之间的随机数:0 <= n < 1.0;
random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。min(a,b) <= n <= max(a,b);
randdom.randint(a, b), 用于生成一个指定范围内的整数,其中a是下限,b是上限: a<= n <= b;
random.randrange(start, stop, step), 从指定范围内,按指定基数递增的集合获取一个随机数;
random.choice(sequence), 从序列中获取一个随机元素;
random.shuffle(x), 用于将一个列表中的元素打乱;
random.sample(sequence, k), 从指定序列中随机获取指定长度的片断;
import random
print random.uniform(1,10)
print random.uniform(10,1)
print random.randint(1,10)
print random.randint(10,10)
print random.randrange(1,10,3)
print random.choice('abflsajldfjsal')
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random.shuffle(list)
print list
slice = random.sample(list, 3)
print slice
print list
运行结果:
3.52553147752
9.32707157558
8
10
1
f
[1, 5, 6, 7, 2, 4, 3, 8, 10, 9]
[1, 5, 4]
[1, 5, 6, 7, 2, 4, 3, 8, 10, 9]