random module

Get to know random module

The random module contains functions that return random numbers, which can be useful for simulations or any program that generates random output.

The following table has some important functions in the random module.

Function Description
random() Returns a random real number n such that 0 <= n < 1
getrandbits(n) Returns n random bits, in the form of a long integer
uniform(a, b) Returns a random real number n such that a <= n < b
randrange([start], stop, [step]) Returns a random number from range(start, stop, step)
choice(seq)Returns a random element from the sequence seq
shuffle(seq[, random]) Shuffles the sequence seq in place
sample(seq, n) Chooses n random, unique elements from the sequence seq

random.randrange

The function random.randrange is the standard function for generating a random integer in the range.

For example, to get a random number in the range from 1 to 10 (inclusive), you would use randrange(1,11) (or, alternatively, randrange(10)+1), and if you want a random odd positive integer lower than 20, you would use randrange(1,20,2).


import random# from   w  w w . ja  v a  2  s . c om

# generate random numbers 1 - 6
die1 = random.randrange(6) + 1    
die2 = random.randrange(6) + 1

print die1 
print die2

The code above generates the following result.

Random integers produced by randrange.


import random

for i in range( 1, 21 ):
   print ( random.randrange( 1, 7 ) )

The code above generates the following result.

Roll a six-sided die 6000 times.


import random# from  w w  w.j av  a2  s  .  c  o  m

frequency1 = 0
frequency2 = 0
frequency3 = 0
frequency4 = 0
frequency5 = 0
frequency6 = 0

for roll in range( 1, 6001 ):       # 6000 die rolls
   face = random.randrange( 1, 7 )

   if face == 1:                    # frequency counted
      frequency1 += 1
   elif face == 2:
      frequency2 += 1
   elif face == 3:
      frequency3 += 1
   elif face == 4:
      frequency4 += 1
   elif face == 5:
      frequency5 += 1
   elif face == 6:
      frequency6 += 1
   else:                            # simple error handling
      print "should never get here!"

print "Face %13s" % "Frequency"
print "   1 %13d" % frequency1
print "   2 %13d" % frequency2
print "   3 %13d" % frequency3
print "   4 %13d" % frequency4
print "   5 %13d" % frequency5
print "   6 %13d" % frequency6

The code above generates the following result.

Random int value


import random#  w  w  w. ja v  a  2  s . co  m

random.random()
random.random()

print random.randint(1, 10)
print random.randint(1, 10)

The code above generates the following result.

Random float


import random
print random.random()    # random float

The code above generates the following result.

Random string value


import random
print random.choice(['apple', 'pear', 'banana'])

The code above generates the following result.

Sampling without replacement


import random
print random.sample(xrange(100), 10)   # sampling without replacement

The code above generates the following result.





















Home »
  Python »
    Language Basics »




Python Basics
Operator
Statements
Function Definition
Class
Buildin Functions
Buildin Modules