The random moduleΒΆ

We often want to use random numbers in programs. Here are a few typical uses:

Python provides a module random that helps with tasks like this.




(chmodule_rand)

Press the run button a number of times. Note that the values change each time. These are random numbers.




(chmodule_rand2)

It is important to note that random number generators are based on a deterministic algorithm — repeatable and predictable. So they’re called pseudo-random generators — they are not genuinely random. They start with a seed value. Each time you ask for another random number, you’ll get one based on the current seed attribute, and the state of the seed (which is one of the attributes of the generator) will be updated. The good news is that each time you run your program, the seed value is likely to be different meaning that even though the random numbers are being created algorithmically, you will likely get random behavior each time you execute.

Next Section - Exercises