Navttc Python Lecture 2C
Navttc Python Lecture 2C
& Applications
Python Random Module
■ Python has a built-in module called random that can be used to make
random numbers:
■ The random module has a set of different methods like randrange(),
randint(), choice(), uniform() etc.
randrange() method
■ The randrange() method returns a randomly selected element
(integers) from the specified range.
■ Syntax
■ random.randrange(start, stop, step)
– Start: Optional. An integer specifying at which position to start.
Default is 0
– Stop: Required. An integer specifying at which position to end.
– Step: Optional. An integer specifying the incrementation. Default is
1
import random
print("Random Number: ",random.randrange(0,10))
Output
Random Number: 7
randint()
■ randint() Returns a random number between the given range
■ Syntax: random.randint(start, stop)
– Start: Required. An integer specifying at which position to start.
– Stop: Required. An integer specifying at which position to end.
import random
print(random.randint(5, 10))
Output
7
getrandbits()
■ getrandbits() Returns a number representing the
random bits. This method returns an integer in the specified size (in
bits).
■ Syntax: random.getrandbits(n)
– N: Required. A number specifying the size, in bits, of the
returned integer.
import random
print(random.getrandbits(2))
Output
3
choice()
■ choice() Returns a random element from the given sequence.
The sequence can be a string, a range, a list, a tuple or any other kind
of sequence.
■ Syntax: random.choice(sequence)
import random
mylist = ["A", "B", "C"]
print(random.choice(mylist))
Output
B
uniform()
■ uniform() The uniform() method returns a random floating
number between the two specified numbers (both included).
■ Syntax: random.uniform(a, b)
– A: Required. A number specifying the lowest possible
outcome
– B: Required. A number specifying the highest possible
outcome
import random
print(random.uniform(0, 5))
Output
3.8074497520247557
■ And many more
random()
■ The random() method returns a random floating number between 0
and 1.
■ Syntax: random.random()
import random
print(random.random())
■ Output
■ 0.3356126402234648
shuffle()
■ The shuffle() method takes a sequence, like a list, and reorganize the
order of the items.
■ Syntax: random.shuffle(sequence)
import random
mylist = ["a", "b", "c", 5]
random.shuffle(mylist)
print(mylist)
Output
['b', 'c', 'a', 5]
sample()
■ The sample() method returns a list with a specified number of
randomly selected items from a sequence.
■ Syntax: random.sample(sequence, k)
– Sequence: Required. A sequence. Can be any sequence: list,
set, range
– K: Required. The size of the returned list
import random
mylist = ["a", "b", "c", 5]
print(random.sample(mylist, k=2))
Output
[5, 'a']
Exercise
1. Write a program to generate a random number between 0 and 20
2. You have five names in a set like
students= [‘Ali’, ‘Zia’, ‘Waseem’, ‘Shakir’, ‘Muneeb’]
Write a random code generator to pick a name randomly.
3. Consider the above list, write a random code generate to select more
than one names.
4. Write a simple program for “Dice Rolling Game”