Generate random numbers.
random
contains generators for the most common distributions.
yes
One most wanted function is to create random integers in a given range:
dice = random.randint(1,6)
The random()
functin generates float numbers between 0 and 1:
import random
print random.random()
import random
random.randint(1,6)
random.random()
random.gauss(0.0, 1.0)
data = [1, 2, 3, 4]
random.shuffle(data)
Random combinations of elements with repetition:
from random import choice
bases = ['A','C','G','T']
dna = [choice(bases) for i in range(20)]
print ''.join(dna)
When elements are to be picked without repetition, you would use, the sample
function:
from random import sample
flavors = ['vanilla','banana','mint']
icecream = sample(flavors, 2)