From the course: Python Standard Library Essential Training (2019)

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Random sequence operations

Random sequence operations

- [Instructor] A common use case for random number generation is to use the generated random number along with a sequence of other values. So for example, you might want to select a random element from a list or a set of other elements. The Python Standard Library makes this process straightforward by providing some convenience functions for these operations. So I've opened up my randseq_start file, and let's try some of these out. So first up is the choice function, which selects a single random element from a sequence such as a list. So suppose we have a short list, such as this one, which contains the possible moves for a rock paper scissors game. So to select a random element, we can use the choice function like this. Print random.choice, and then we'll pass it our list of moves, so let's run that a few times. Alright so there's rock, there's scissors, there's scissors again, there's rock again, and there's paper. Okay so you could see that different elements are chosen each time.…

Contents