python logo

random number


Python hosting: Host, run, and code Python in the cloud!

Generating random numbers is a common task in Python. Whether it’s for simulations, gaming, or even testing, Python’s random module offers versatile functions to cater to different needs. This guide will explore the various methods to generate random numbers in Python.

Using the random module in Python, you can produce pseudo-random numbers. The function random() yields a number between 0 and 1, such as [0, 0.1 .. 1]. Although numbers generated using the random module aren’t truly random, they serve most use cases effectively.

Related Course: Python Programming Bootcamp: Go from zero to hero

Generating a Random Float Between 0 and 1

With just a few lines of code, you can generate a pseudo-random floating-point number. Here’s how:

1
2
3
from random import random

print(random()) # Outputs a pseudo-random float between 0 and 1.

Producing a Random Integer Between 1 and 100

If you need a whole number or integer between 1 and 100, use the following approach:

1
2
3
from random import randint

print(randint(1, 100)) # Generates and displays a random integer between 1 and 100.

To store this random integer in a variable, follow this example:

1
2
3
4
from random import randint

number = randint(1, 100) # Stores the random number between 1 and 100 in the variable 'number'.
print(number)

Getting a Random Float Between 1 and 10

To generate a random floating point number between 1 and 10, the uniform() function is the right tool.

1
2
3
from random import uniform

print(uniform(1, 10)) # Outputs a random float between 1 and 10.

Manipulating Lists with Randomness

Lists are versatile data structures in Python, and sometimes you may want to introduce randomness when working with them.

Shuffling a List:
To randomize the order of items in a list, use the following:

1
2
3
4
5
from random import shuffle

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
shuffle(numbers)
print(numbers) # Displays the shuffled list.

Selecting Random Items from a List:
For selecting a single or multiple random items from a list, observe these examples:

1
2
3
4
5
6
7
8
9
from random import sample

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

single_item = sample(numbers, 1)[0] # Picks and stores one random item from the list.
print(single_item)

multiple_items = sample(numbers, 4) # Chooses and stores four random items from the list.
print(multiple_items)

The same method works for lists containing strings:

1
2
3
4
5
6
7
8
9
from random import sample

names = ['Alissa', 'Alice', 'Marco', 'Melissa', 'Sandra', 'Steve']

single_name = sample(names, 1)[0] # Picks one random name from the list.
print(single_name)

multiple_names = sample(names, 4) # Chooses four random names from the list.
print(multiple_names)

To get more practice with Python, you can download these Python exercises.

Navigate through our tutorials: < Back | Next >






Leave a Reply:




Chi Ngo Mon, 08 Jun 2015

the function "sample" does not work on my laptop with windows9

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

Frank Mon, 08 Jun 2015

Which Python version are you using? Try replacing by print(x[0])

Hari Tue, 30 Jun 2015

Im getting error in randint( )

>>> from random import *
>>> print randint(1,100)
SyntaxError: invalid syntax
I'm using Python 3.4.3

Frank Tue, 30 Jun 2015

Hi Harsi, in Python 3 you need bracket around a print statement. Try this:

>>> from random import *
>>> print(randint(1,100))

Adam Tue, 30 Jun 2015

Hmm could you guys tell me what does it: from random import * mean ?
Thanks :D

Frank Tue, 30 Jun 2015

Hi, this imports all the functions from a python file called random (actually random.py.

This file is one of the standard python modules. You can see the source code of random.py here: https://hg.python.org/cpython/file/2.7/Lib/random.py.

You can create your own module that way:

Create a file called test.py

#!/usr/bin/env python

def add(a,b):
return a+b

Then create a file called app.py:

from test import *

print('hello')
print(add(5,2))

The program app.py now uses the code in test.py. The same principle applies when you use "from random import *"

Gamal Fri, 18 Sep 2015

from random import *

items = ['Alissa','Alice','Marco','Melissa','Sandra','Steve']
x = sample(items, 1) # Pick a random item from the list
print x[0]
why we put [0] here ?
and what will happen if we change (items, 1) to (items, 2 or 3 or 4 )
In the presence of print x[0]
This point needs to explain more...

Frank Fri, 18 Sep 2015

The sample function can return a list of numbers. sample(items,2) would return a list of two random numbers. x[0] is simply the first random number in the list

Steve Wed, 28 Oct 2015

Is the statement from random import * a seeding process?
Does this mean that y = sample(items,4) will return the same 4 items each time it is used?

Frank Wed, 28 Oct 2015

Hi Steve, the statement y = sample(items,4) will return new items every call. If you want you can call the random class seed() function, which uses the system time to initialize.

Gauge Thu, 05 May 2016

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.

Frank Thu, 05 May 2016

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


seq = [1,2,3,4,5]
y = seq * randomElement

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


from random import *
items = [1, 2.13, 3.24, 4.25, 5.46, 6.57, 7.68, 8.79, 9.810, 10.911]
seq = [1,2,3,4,5]
# Get a list of 1 random numbers, convert to one random number.
randomElement = sample(items, 1)
randomElement = randomElement[0]
# Multiple every element of the list
for element in seq:
y = element * randomElement
print("y=", y)


Alternative is to use one of the two methods below:


from random import *
seq = [1,2,3,4,5]
# Get a list of 1 random numbers, convert to one random number.
randomElement = sample(seq, 1)
randomElement = randomElement[0]
# Multiple every element of the list, using list comprehension
l = [randomElement*x for x in seq]
print(l)
# Multiple every element using map
l = list(map(lambda x:randomElement*x, seq))
print(l)

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

Gauge Thu, 05 May 2016

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.

Gauge Fri, 06 May 2016

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.

Frank Fri, 06 May 2016

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 measuring
These are the functions inside the Python code:
(however depending on your version of Python they may have a different implementation)


def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
try:
i = self._randbelow(len(seq))
except ValueError:
raise IndexError('Cannot choose from an empty sequence')
return seq[i]
def sample(self, population, k):
if isinstance(population, _collections.Set):
population = tuple(population)
if not isinstance(population, _collections.Sequence):
raise TypeError("Population must be a sequence or Set. For dicts, use list(d).")
randbelow = self._randbelow
n = len(population)
if not 0 <= k <= n:
raise ValueError("Sample larger than population")
result = [None] * k
setsize = 21 # size of a small set minus size of an empty list
if k > 5:
setsize += 4 ** _ceil(_log(k * 3, 4)) # table size for big sets
if n <= setsize:
# An n-length list is smaller than a k-length set
pool = list(population)
for i in range(k): # invariant: non-selected at [0,n-i)
j = randbelow(n-i)
result[i] = pool[j]
pool[j] = pool[n-i-1] # move non-selected item into vacancy
else:
selected = set()
selected_add = selected.add
for i in range(k):
j = randbelow(n)
while j in selected:
j = randbelow(n)
selected_add(j)
result[i] = population[j]
return result


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:


from random import *
import time
def testSample(size):
start_time = time.time()
seq = [1,2,3,4,5]
for i in range(0,size):
randomElements = sample(seq, 1)[0]
print("--- %s seconds ---" % (time.time() - start_time))
def testChoice(size):
start_time = time.time()
seq = [1,2,3,4,5]
for i in range(0,size):
randomElement = choice(seq)
print("--- %s seconds ---" % (time.time() - start_time))
testSample(1000000)
testChoice(1000000)

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)

Gauge Fri, 06 May 2016

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,

Frank Sat, 07 May 2016

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:


def choice(self, seq):
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
def sample(self,population,k):
-snip-
j = _int(random() * (n-i))
-snip-

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

Tim Osborn Sat, 03 Jun 2017

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