Random numbers


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:

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

Datatype casting
Read file

18 thoughts on “Random numbers

  1. Steve - October 28, 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?

    1. Frank - October 28, 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.

  2. Gamal - September 18, 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…

    1. Frank - September 18, 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

  3. Adam - June 30, 2015

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

    1. Frank - June 30, 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 *”

  4. Hari - June 30, 2015

    Im getting error in randint( )

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

    1. Frank - June 30, 2015

      Hi Harsi, in Python 3 you need bracket around a print statement. Try this:
      >>> from random import *
      >>> print(randint(1,100))

  5. Chi Ngo - June 8, 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]

    1. Frank - June 8, 2015

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