Using the *random* module, we can generate pseudo-random numbers. The function random() generates a random number between zero and one [0, 0.1 .. 1]. Numbers generated with this module are not truly random but they are enough random for most purposes.

**Related Courses:**

- Complete Python Bootcamp: Go from zero to hero in Python
- Automate the Boring Stuff with Python Programming

**Random number between 0 and 1.**

We can generate a (pseudo) random floating point number with this small code:

from random import * print random() # Generate a pseudo-random number between 0 and 1. |

**Generate a random number between 1 and 100**

To generate a whole number (integer) between one and one hundred use:

from random import * print randint(1, 100) # Pick a random number between 1 and 100. |

This will print a random integer. If you want to store it in a variable you can use:

from random import * x = randint(1, 100) # Pick a random number between 1 and 100. print x |

**Random number between 1 and 10**

To generate a random* floating point number* between 1 and 10 you can use the uniform() function

from random import * print uniform(1, 10) |

Picking a random item from a list

**Fun with lists**

We can shuffle a list with this code:

from random import * items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] shuffle(items) print items |

To pick a random number from a list:

from random import * items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] x = sample(items, 1) # Pick a random item from the list print x[0] y = sample(items, 4) # Pick 4 random items from the list print y |

We can do the same thing with a list of strings:

from random import * items = ['Alissa','Alice','Marco','Melissa','Sandra','Steve'] x = sample(items, 1) # Pick a random item from the list print x[0] y = sample(items, 4) # Pick 4 random items from the list print y |

Quick thanks for the great comments thread – it elevated a my super basic reference search to a whole ‘nother level – kudos!

Just a quick question about sample() — when used to fill values in an equation it returns:

TypeError: can’t multiply sequence by non-int of type ‘float’

I’m curious why this isn’t allowed, and if there is an alternative that I haven’t been able to find for extracting multiple random values from a series of value. For now, all I’ve come up with is using the choice() alternative with a loop for however large my sample pool needs to be, but I have a feeling there’s a better way to do it. Admittedly, I haven’t read through all of the documentation for random thoroughly, but I haven’t found a better solution. I figured that someone with a bit of knowledge would be my best bet. Any suggestions? As an aside, I should note I’m using 2.7 to learn with. I know they changed some stuff with Python 3.x, so I’m not sure if this is even an issue with newer revisions.

Are you multiplying with a sequence, perhaps something like this?:

To do so, we have to iterate over every element:

Alternative is to use one of the two methods below:

I hope that helps. If not, could you post the code?

I think you answered my question pretty well. My mistake was in not addressing the derived sample with an index value, so I was essentially attempting mathematics against a list rather than single values from within the list. It somehow slipped past me that sample returned a list. I guess it’s a beginner mistake and lack of attention on my part.

Quick questions, though:

Is the behavior of a loop iteration of choice() essentially the same as the returned sample() list? For clarification, a simple example would be a loop that iterates 4 times using choice() and storing the returned values in a list, versus a sample() of 4 the same values. Would that essentially return the same chance of random values?

Also, supposing they essentially return the similar randomness in their values, is there a performance difference over larger iterations? Common sense tells me that since the looping choice() jumps right to the chase, but a sample() first stores the values in a new list and that list then needs to be iterated over, looping choice() would be the cleaner and more efficient alternative. Am I mistaken?

Thanks for the quick response, I appreciate your answer and it definitely helped me understand where I originally went wrong.

Apologies for the double response. The original response didn’t exist on a new browser, and the original browser was stuck on the spinning dots for the comment section, so I assumed it hadn’t gone through the first time. Feel free to delete whichever one you think is best.

No problem.

Practically that would do the same. There is a slight difference, choice() will raise an IndexError if the sequence is empty. Sample will throw a ValueError. This matters if you implement error handling (try/catch statement). Performance wise there is a big difference.

Performance measuringThese are the functions inside the Python code:

(however depending on your version of Python they may have a different implementation)

Notice that the sample() function executes a lot more operations. My gut feeling tells that the choice() function would be smaller, but let’s put that to the test.

I came up with this program:

Output with Python 2.7:

— 5.25197696686 seconds —

— 1.08564114571 seconds —

Output with Python 3.4:

— 17.56459665298462 seconds —

— 2.1325480937957764 seconds —

I would say you are correct, choice() is faster.

In terms of randomness, all of the numbers generated by the random module as pseudo-random. Python 2.3 uses the Wichmann-Hill algorithm , later versions use the MersenneTwister algorithm (Python implementation below)

I’m assuming your response hit the maximum depth, so I’ll respond to myself here for the sake of keeping it together…

I appreciate your response, and I’m actually relieved that my line of thought seems to be heading in the right direction. I just had a finishing question for clarity.

If I’m understanding you right, each time a number is selected, no matter which version of “random” you use, it runs the same algorithm at the core to return each value with additional modification for specific behavior. Meaning, the returned values essentially exist in the same frequencies of pseudo-randomness? And,

Yes, you are understanding right. The functions choice() and sample() use the same algorithm, which is implemented in the method random(). It is called indirectly from the method randbelow().

Python 2.7 would simply call the algorithm (random()) directly:

Is there a question after “And,” ? It seems to have gone missing.