前提:需要导入random模块 >>>import random 1、产生 0.0 <= x < 1.0的随机小数 >>> random.random() # Random float x, 0.37444887175646646 2、产生 1.0 <= x < 10.0的随机小数 >>> random.uniform(1, 10) # Random float x, 1.1800146073117523 3、随机产生10到100的整数 >>> random.randint(10, 100) 56 4、选择一个随机元素 >>> random.choice('abcdefghij') # Choose a random element 'c' 5、洗牌: >>> items = ['a','b','c','d','e','f'] >>> random.shuffle(items) >>> items ['e', 'c', 'b', 'd', 'f', 'a'] 6、从一个列表中选择指定个数的元素组成新列表 >>> random.sample([1, 2, 3, 4, 5], 3) # Choose 3 elements [4, 1, 5] 多个字符中选取特定数量的字符组成新字符串: >>> import random >>> import string >>>sample_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'] >>>string.join(random.sample(sample_list,6)).replace(" ","") 'kqf2bl' 参考资料:http://docs.python.org/library/random.html