[Go to site: main page, start]

0% found this document useful (0 votes)
4 views4 pages

Using Random Module

The document discusses the use of the Python random module for generating random numbers in various applications such as games and simulations. It outlines key functions like randrange(), random(), randint(), and others, providing syntax and examples for each. Additionally, it explains the significance of the seed function for initializing the pseudorandom number generator.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Using Random Module

The document discusses the use of the Python random module for generating random numbers in various applications such as games and simulations. It outlines key functions like randrange(), random(), randint(), and others, providing syntax and examples for each. Additionally, it explains the significance of the seed function for initializing the pseudorandom number generator.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Using Random Module

There are certain situations/applications that involve games or simulations which


work on a non-deterministic approach. In these types of situations, random
numbers are extensively used such as:
1. Pseudorandom numbers on lottery scratch cards.
2. Captcha (like in login forms) uses a random number generator to define which
image is to be shown to the user.
3. Computer games involving throwing of a dice, picking a number, or flipping a
coin.
4. Shuffling deck of playing cards, etc.

Using Python’s random number library


 Random module used to generate randome numbers within the specified
range.
 To generate random number in Python we must import random module
 3 most common method to generate random number in python are :
 random() function
 randint(a,b) function
 randrange(a,b) function
 Functions in the random module depend on a pseudo-random number
generator function random(), which generates a random float number
between in the range [0.0, 1.0).

Functions of random module

1. Randrange(): This method generates an integer between its lower and


upper arguments. By default lower argument is 0. Takes one to three
arguments and returns an integer:
• Syntax:
• randrange(stop):
between zero (inclusive) and stop (exclusive! Not including stop)
• randrange(start, stop): [start, stop)
Or
• [Link](start, stop [, step])
between start (inclusive) and stop (exclusive)
• randrange(start, stop, step):
Likewise, but only gives start plus a multiple of step. For
example, [Link](2, 20, 2) will return any random number
between 2 to 20, such as 2, 4, 6, …18.

randrange() example:

KOUSHILYA PATEL,PGT CS,KVCME,PUNE-31 1


import random print("Random integer is",
[Link](2, 20, 2))

2. random() : This generates floating value between 0 and 1. it does not


require any argument.
Example:

3. randint ()

syntax: randint (a, b):


• Inclusive on both ends! The same as randrange (a, b+1)
• Always has two arguments, no more, no less
• Returns numbers in a range starting at a, up to and including b
• This may return any number between these two numbers including both
limits. This method is very useful for guessing applications.
Examples:

10
Output- 5
1 4
2
9
10
2
3
5
6

4. uniform (): This method return any floating-point number between two
given numbers.
Syntax: [Link](self,a,b)
where ‘a’ and ‘b’ specify the range and self is an imaginary parameter.
Examples:

KOUSHILYA PATEL,PGT CS,KVCME,PUNE-31 2


5. choice ()

[Link](seq): Use the [Link] function to randomly select


an item from a List or any sequence.

 Don’t forget the brackets!


 Must give a list of choices, in square brackets.
 Can give a string as an argument instead as

Other form->

Example 2:

6. shuffle ():this method can shuffle or swap the items of a given list.
Syntax: [Link](x[, random])
Use this function to shuffle or randomize a list or other sequence types.
The shuffle function shuffles a list in-place. The most common example is
shuffling cards.
Example 1 :

KOUSHILYA PATEL,PGT CS,KVCME,PUNE-31 3


list = [2,5,8,9,12] [Link](list)
Example 2 :
print ("Printing shuffled list ", list)

Output:
Printing shuffled list [5, 9, 12, 2, 8]

7. [Link](population, k)
Use this function when you want to pick multiple elements from a list
or any sequence randomly.
Example:

import random city_list = ['New Delhi’, ’Pune', ‘Chennai', ‘Hydrabad',


‘Mumbai’]
print("Pick 2 Random element from list:", [Link](city_list, 2))

8. [Link](a=None, version=2)
The seed function is used to initialize the pseudorandom number
generator in Python. The random module uses the seed value as a base to
generate a random number. If seed value is not present, it takes a system
current time. If you use the same seed value before calling any random module
function, you will get the same output every time.
Example:
import random
# Random number with seed
6 Output:
[Link](6) 19
print([Link](10, 20)) 19
[Link](6)
print([Link](10, 20))

KOUSHILYA PATEL,PGT CS,KVCME,PUNE-31 4

You might also like