ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:glossary:p:probability:start

Theory of probability

Theory of probability is a fascinating topic by itself and get becomes even more fascinating if you apply it to games.

Recommended reading:

To apply it to games with python, you need to import python's random module (shipped with python). To simulate the throw of a six-sided dice, write:

>>>import random
>>>result = random.randint(1,6) # lower bond, upper bond
>>>print result
2

You can find out all functions of the random module by typing at the python prompt:

>>>import random
>>>help("random")

Some useful functions tend to use frequently are:

  • random.randint(a, b) creates a random integer between (including) a and b
  • radnom.random() creates a float between 0 and 1
  • random.choice(list) randomly choose one element of a list
/var/www/horst/thepythongamebook.com/data/pages/en/glossary/p/probability/start.txt · Last modified: 2020/05/13 06:55 by Horst JENS