[python学習」randomモジュールの乱数を生成する方法まとめ
1.random()
乱数範囲: 0 <= n < 1.0
例:
>>> print random.random()
0.283212844881
2.uniform()
例:
>>> print random.uniform(1,10)
1.2558047703
>>> print random.uniform(10,1)
6.78930988147
3.randint()
#print random.randint(10, 20)
4.randrange()
形式 randrange([start],stop[, step])
例:random.randrange(10, 100, 2)
5.choice()
例:
print random.choice(“arkgame.com")
print random.choice([“startnews24", “is", “a", “it", “site"])
print random.choice((“soft", “code", “in arkgame.com"))
6.shuffle()
形式 shuffle(x[,random]
例:
>>> p = ['a’,’b’,’c’]
>>> random.shuffle(p)
>>> print p
['a’, 'c’, 'b’]
7.sample()
形式 random.sample(sequence,k)
例
>>> print random.sample(['a_startnews24′,’b_startnews24′,’c_startnews24′,’d’],3) ['a_startnews24’, 'b_startnews24’, 'c_startnews24’]