Don't forget to also checkout my second blog containing articles to all other related ICT topics!!

Thursday, May 17, 2012

Programming under uncertainty

If you need to write a program that deals with stochastic events, you should definitely take a look at the random module. It offers a lot of nice methods to generate random elements. Just to show a few examples:
import random
for i in range(0,4):
    print random.choice(['head', 'tails'])
print '----------------'
for i in range(0,4):
    print random.randint(3,6)
print '----------------'
for i in range(0,4):
    print random.randrange(2,15,3)

head
tails
head
head
----------------
4
6
3
6
----------------
5
14
2
8

No comments:

Post a Comment