Three Gems from the Python random library

Python code showing that using the same seed value gives the same random numbers

Python is a batteries-included language, meaning it includes many many useful features in the standard library included with your Python installation. One of the handy libraries israndom that offers a few convenient functions for doing pseudo-random things.

Why pseudo-random? Well, computers are pretty much dependent on being told what to do, and you can’t tell them to do something that is actually random. So, instead, they use some very fancy maths to generate a number that is effectively random enough.

If you want to pull back the curtain, you can tinker with setting the seed value. You will find that for a given seed value, you will get the same progression of random numbers. This is shown in the following example.

So, you have probably used random.randint() or random.random() to get a random number to use in your code before, but maybe you will be as pleasantly surprised as I was by some of the other gems in there. They make your code more readable, the intent that much clearer, and they save you doing the error-prone hard work yourself.

Here are three lovely functions for working with sequences.

#1 Choice

Are you looking for a random choice from a sequence of items? Do you work out the length of the sequence, generate a randint for that length to get an index and then look up that item in the sequence?

Then random.choice(seq) is for you. If you pass your sequence to thechoice function, it will do all of that for you and return you the element it has picked.

Your code also reads really nicely now, as the logic for selecting a choice is encapsulated in a method with just the right name.

#2 Shuffle

Again, shuffle saves you a tonne of logic and gives a super intuitive way to shuffle a sequence in place. This means that the actual sequence you pass in to the method is shuffled. If you needed to keep a record of the original order, that must be done separately.

For example, you could shuffle a deck of cards like this:

#3 Sample

Sample is probably my favourite. If you wanted two or three random elements from a sequence, and you want to make sure you don’t pick the same element more than once, random.sample(population, k) is the function for you. It takes your sequence as the population argument, and the number of items you would like picked from it as the k argument.

The following example shows a simple lottery number generator. You can only pick each number once, and again, this function saves you the heavy lifting involved with tracking your guesses and removing duplicates.

So there you have it, I suggest you fire up a terminal now and have a play with choice, shuffle and sample, and enjoy more readable, more pyhtonic random behaviour in your code.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store