SlideShare a Scribd company logo
13
What if you want to generate a random number of length n? For example, you want
to generate a random number of length 3 means (100,999)
import random
num1 = random.randrange(100, 1000)
print("First random number of length 3 is", num1)
----Output-------
Random integer: 160
Generate a random negative integer
Let’s see how to generate a random negative integer between -15 to -5.
import random
k = random.randrange(-15, -5)
print("Random no:“,k)
----Output-------
Random no:
Most read
15
random.randint()
Syntax:
random.randint(start, stop)
This function returns a random integer within a range. This function takes two
parameters. Both are mandatory. It will generate a random number from the
inclusive range.
import random
print("Random integer from 0 to 9")
k = random.randint(0, 9)
print("Random no: ", k)
----Output-------
Random no:5
Most read
18
What possible outputs(s) of the following code? Also specify the maximum and minimum
values that can be assigned to variable NUM
import random
NAV=[“LEFT”,”FRONT”,RIGHT”,”BACK”];
NUM=random.randint(1,3)
NAVG=“”
for C in range(NUM,1,-1):
NAVG=NAVG+NAV[C]
print (NAVG)
(i) BACKRIGHT (ii) BACKRIGHTBACK
(iv) BACK
(iii) LEFTFRONTRIGHT
0 1 2 3 4
0 L E F T
1 F R O N T
2 R I G H T
3 B A C K
NUM
321
CONDITIONS:
IF randint() generate 1 then range(1,1,-1)
means 0, it does not run
IF randint() generate 2 then range(2,1,-1)
means 2 only, “RIGHT”
IF randint() generate 3 then range(3,1,-1)
means 3,2 only, “RIGHT” ,”BACK”
13
Most read
FUNCTIONS IN PYTHON
COMPUTER SCIECNCE(083)
CLASS: XII
RANDOM FUCTION IN
PYTHON
PART-2
RANDOM MODULE
To use random functions import random.py module inside your python program
import random
Python has a built-in module that you can use to make random numbers.
random() randrange() randint()
uniform() shuffle() choice()
random()
This function generates a random float number between 0.0 and 1.0.It
include 0, but excluding 1).
Example: The random numbers generate by random like:
0.564388123456754
Every time we execute the random() it will generate different floating
number.
import random
print("Printing random number: ”,random.random())
----OUTPUT----
Printing random number: 0.07252177092679812
random()
This function generates a random float number between 0.0 and 1.0.It
include 0, but excluding 1).So now you know that it generates number
between 0.0 to 1.0 ( excluding 1).
But if you want the number generate by this random() function between 0
to 5. It means 0 is the lowest number and 4 point something is the highest
number.It is due to 0.0 *5 is 0.0 and 1.0 * 5 is 5.0(excluding 5) means 4.0
import random
k=random.random()* 5
print(“Random num:”,k)
--output---
Random num:0.6715376992415667
--output---
Random num:2.339768894331859
--output---
Random num:1.6150877389220542It generate number from 0.0 to 4.000
random()
Now you know that it generate number from 0 to less than 5 if we execute
above line but in floating value.If we need the result in integer format than,
we need to use int() to convert floating value to integer.
k=random.random()* 5
import random
k=random.random()* 5
print(“Random num:”,int(k))
--output---
Random num: 1
--output---
Random num: 2
--output---
Random num: 4
--output---
Random num: 0
It include lowest number
that is by default 0 and
exclude the highest number
random()
Now if we want to generate numbers not from 0 but from 7 to 15 then?
k=random.random()* 15 + 7
After +(Plus) sign the value
is the lowest means 7 is the
lowest value
Before +(Plus) sign the value is
Highest means random()* 15 is
the highest value
So number should generate from 7 included and up to 14 (15 excluded)
But it will give some different output, that not match with the result
we need:
--output---
Random num: 12
k=random.random()* 15 + 7
import random
print(“Random num:”,int(k))
--output---
Random num: 7
--output---
Random num: 11
--output---
Random num: 21
--output---
Random num: 15
--output---
Random num: 21
--output---
Random num: 19
So why numbers not generated up to 15 it is due
to the method that you need to learn.So if we
solve the line random()*15 + 7 means numbers
highest number is 14 means from 0 to 14 but
when we add 7, we get number from 7 to 21.
--output---
Random num: 12
k=random.random()* 9 + 7
import random
print(“Random num:”,int(k))
--output---
Random num: 15
--output---
Random num: 7
--output---
Random num: 14
Question is how we will get the result, we need that is :
From 7 to 15
Lowest number is 7 and how we get highest number
will be =HIGH – LOW + 1 =15-7=8+1=9
So now lowest no. is 7 and high
no. is 9+7-1=15,random no.
from 7 to 15
k=random.random()* 111 + 75
import random
print(“Random num:”,int(k))
Question : To generate the number from 75 to 185
Lowest number is 75 and how we get highest number will be =HIGH – LOW
=185-75 + 1=110+1=111
Question : To generate the number from 99 to 999
Lowest number is 75 and how we get highest number
will be =HIGH – LOW + 1=999-99 + 1=901
k=random.random()* 901 + 99
--output---
Random num: 90
print(“Random num:”,int(k))
--output---
Random num: 158
randrange() It returns a random number between the given range
Syntax random.randrange(start, stop, step)
Parameter Values
Parameter Description
start
stop
step
Optional. An integer specifying at which position to start.
Default 0
Required. An integer specifying at which position to end.
Optional. An integer specifying the incrementation.
Default 1
Question : To generate the random number between 0 and 39 using random.range()
import random
# Random number between 0 and 39
num1 = random.randrange(40)
print("Random integer: ", num1)
----Output------
Random integer: 7
import random
# Random number between 20 and 39
num2 = random.randrange(20, 40)
print("Random integer: ", num2)
Question : To generate the random number between 20 and 39 using random.range()
----Output------
Random integer: 34
import random
# Random number between 25 and 249 divisible by 5
num3 = random.randrange(25, 250, 5)
print("Random integer: ",num3)
----Output-------
Random integer: 170
Question : To generate the random number between 25 and 249 divisible by 5
using random.range()
import random
num3 = random.randrange(2, 40, 2)
print("Random integer: ", num3)
----Output-------
Random integer: 26
Question : To generate the random number between 2 and 40 divisible by 2
using random.range()
What if you want to generate a random number of length n? For example, you want
to generate a random number of length 3 means (100,999)
import random
num1 = random.randrange(100, 1000)
print("First random number of length 3 is", num1)
----Output-------
Random integer: 160
Generate a random negative integer
Let’s see how to generate a random negative integer between -15 to -5.
import random
k = random.randrange(-15, -5)
print("Random no:“,k)
----Output-------
Random no:
Generate a random integer number multiple of n
In this example, we will generate a random number between 3 to 66, which is a
multiple of 3 like 3, 6, 39, 66.
Generate random positive or negative integer from -10 to 10
import random
num = random.randrange(-10, 10)
print(num)
import random
num = random.randrange(3,66,3)
print(num)
random.randint()
Syntax:
random.randint(start, stop)
This function returns a random integer within a range. This function takes two
parameters. Both are mandatory. It will generate a random number from the
inclusive range.
import random
print("Random integer from 0 to 9")
k = random.randint(0, 9)
print("Random no: ", k)
----Output-------
Random no:5
Random integer from 10 to 100 using random.randint()
num2 = random.randint(10, 100)
print("Random integer: ", num2)
Random integer: 98 Random integer: 15
Random integer 0,1 using random.randint()
num2 = random.randint(0, 1)
print("Random integer: ", num2)
Random integer: 1 Random integer: 0
Program to create random numbers list of 10 numbers from
10 to 100
import random
no = []
for i in range(0, 10):
no.append(random.randint(10, 100))
print("Print list of 10 random numbers")
print(no)
Print list of 10 random numbers
[36, 80, 57, 98, 55, 20, 38, 64, 87, 39]
i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
no.append(random.randint(10,100))
no.append(random.randint(10,100))
no.append(random.randint(10,100))
no.append(random.randint(10,100))
no.append(random.randint(10,100))
no.append(random.randint(10,100))
no.append(random.randint(10,100))
no.append(random.randint(10,100))
no.append(random.randint(10,100))
no.append(random.randint(10,100))
[ ]36 ,80,57,98,55,20,38,64,87,39
What possible outputs(s) of the following code? Also specify the maximum and minimum
values that can be assigned to variable NUM
import random
NAV=[“LEFT”,”FRONT”,RIGHT”,”BACK”];
NUM=random.randint(1,3)
NAVG=“”
for C in range(NUM,1,-1):
NAVG=NAVG+NAV[C]
print (NAVG)
(i) BACKRIGHT (ii) BACKRIGHTBACK
(iv) BACK
(iii) LEFTFRONTRIGHT
0 1 2 3 4
0 L E F T
1 F R O N T
2 R I G H T
3 B A C K
NUM
321
CONDITIONS:
IF randint() generate 1 then range(1,1,-1)
means 0, it does not run
IF randint() generate 2 then range(2,1,-1)
means 2 only, “RIGHT”
IF randint() generate 3 then range(3,1,-1)
means 3,2 only, “RIGHT” ,”BACK”
13
What possible outputs(s) of the following code? Also specify the maximum and minimum
values that can be assigned to variable NUM
import random
P=“MY PROGRAM”
x=0
while(P[x] != ‘R’):
a=random.randint(0,3)+5
print(P[a], “-”,end=“”)
x=x+1
(i) R-P-O-R- (ii) P-O-R-Y-
(iv) A-G-R-M-
(iii) O-R-A-G-
0 1 2 3 4 5 6 7 8 9
M Y P R O G R A M
MIN: 5 MAX: 8
a random no. starts from 5 to 8 means 5,6,7,8
What possible outputs(s) are expected to be displayed on screen at the time of execution
of the program from the following code? Also specify the maximum values that can be
assigned to each of the variables FROM and TO.
import random
AR=[20,30,40,50,60,70];
FROM=random.randint(1,3)
TO=random.randint(2,4)
for K in range(FROM,TO+1):
print (AR[K],end=”#“)
(i) 10#40#70#
(ii) 30#40#50#
(iii) 50#60#70#
(iv) 40#50#70#
FROM random no. 1, 2, 3
TO random no. 2, 3, 4
0 1 2 3 4 5
20 30 40 50 60 70
Now loop starts from FROM(1,2,3)
And Ends at TO+1 means if TO is (2,3,4)
Inside range second value run less than given
number(2 means 1, 3 means 2 and 4 means
3)that why we put TO+1(2,3,4)
If FROM random generate no 1 means AR[1]=
30 or 2 means AR[2]=40 or AR[3]=50
If TO random generate no 2 means AR[2]= 40 or
3 means AR[3]=50 or AR[4]=60

More Related Content

What's hot (20)

Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
VedaGayathri1
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
deepalishinkar1
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
vikram mahendra
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
Emertxe Information Technologies Pvt Ltd
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
Mohd Sajjad
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
Siddique Ibrahim
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
List in Python
List in PythonList in Python
List in Python
Sharath Ankrajegowda
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
VedaGayathri1
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
vikram mahendra
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
Mohd Sajjad
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 

Similar to FUNCTIONS IN PYTHON[RANDOM FUNCTION] (20)

Non sequitur
Non sequiturNon sequitur
Non sequitur
Javier Jair Trejo García
 
Comp102 lec 5.0
Comp102   lec 5.0Comp102   lec 5.0
Comp102 lec 5.0
Fraz Bakhsh
 
CMSC 201 - Lec19 - Modules and Random Numbers.pptx
CMSC 201 - Lec19 - Modules and Random Numbers.pptxCMSC 201 - Lec19 - Modules and Random Numbers.pptx
CMSC 201 - Lec19 - Modules and Random Numbers.pptx
naps73r
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
Magic 8 ball putting it all together
Magic 8 ball  putting it all togetherMagic 8 ball  putting it all together
Magic 8 ball putting it all together
geekinlibrariansclothing
 
17 PYTHON MODULES-2.pdf
17 PYTHON MODULES-2.pdf17 PYTHON MODULES-2.pdf
17 PYTHON MODULES-2.pdf
JeevithaG22
 
PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-
Yoshiki Satotani
 
PY_17_06_20-1.pptx
PY_17_06_20-1.pptxPY_17_06_20-1.pptx
PY_17_06_20-1.pptx
NitinSharma134320
 
python modules1522.pdf
python modules1522.pdfpython modules1522.pdf
python modules1522.pdf
DebanjanMaity13
 
Python Math Concepts Book
Python Math Concepts BookPython Math Concepts Book
Python Math Concepts Book
Rohan Karunaratne
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
Ruth Marvin
 
function_xii-BY APARNA DENDRE (1).pdf.pptx
function_xii-BY APARNA DENDRE (1).pdf.pptxfunction_xii-BY APARNA DENDRE (1).pdf.pptx
function_xii-BY APARNA DENDRE (1).pdf.pptx
g84017903
 
Python in details
Python in detailsPython in details
Python in details
Khalid AL-Dhanhani
 
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docxSkip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
edgar6wallace88877
 
Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On Randomness
Ranel Padon
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
simenehanmut
 
Python summer course play with python (lab1)
Python summer course  play with python  (lab1)Python summer course  play with python  (lab1)
Python summer course play with python (lab1)
iloveallahsomuch
 
Python summer course play with python (lab1)
Python summer course  play with python  (lab1)Python summer course  play with python  (lab1)
Python summer course play with python (lab1)
iloveallahsomuch
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
Abdul Haseeb
 
CMSC 201 - Lec19 - Modules and Random Numbers.pptx
CMSC 201 - Lec19 - Modules and Random Numbers.pptxCMSC 201 - Lec19 - Modules and Random Numbers.pptx
CMSC 201 - Lec19 - Modules and Random Numbers.pptx
naps73r
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
17 PYTHON MODULES-2.pdf
17 PYTHON MODULES-2.pdf17 PYTHON MODULES-2.pdf
17 PYTHON MODULES-2.pdf
JeevithaG22
 
PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-
Yoshiki Satotani
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
Ruth Marvin
 
function_xii-BY APARNA DENDRE (1).pdf.pptx
function_xii-BY APARNA DENDRE (1).pdf.pptxfunction_xii-BY APARNA DENDRE (1).pdf.pptx
function_xii-BY APARNA DENDRE (1).pdf.pptx
g84017903
 
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docxSkip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
edgar6wallace88877
 
Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On Randomness
Ranel Padon
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
simenehanmut
 
Python summer course play with python (lab1)
Python summer course  play with python  (lab1)Python summer course  play with python  (lab1)
Python summer course play with python (lab1)
iloveallahsomuch
 
Python summer course play with python (lab1)
Python summer course  play with python  (lab1)Python summer course  play with python  (lab1)
Python summer course play with python (lab1)
iloveallahsomuch
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
Abdul Haseeb
 
Ad

More from vikram mahendra (20)

Communication skill
Communication skillCommunication skill
Communication skill
vikram mahendra
 
Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop system
vikram mahendra
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shop
vikram mahendra
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEM
vikram mahendra
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Python
vikram mahendra
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
vikram mahendra
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
vikram mahendra
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
vikram mahendra
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
vikram mahendra
 
GREEN SKILL[PART-2]
GREEN SKILL[PART-2]GREEN SKILL[PART-2]
GREEN SKILL[PART-2]
vikram mahendra
 
GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]
vikram mahendra
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
vikram mahendra
 
Entrepreneurial skills
Entrepreneurial skillsEntrepreneurial skills
Entrepreneurial skills
vikram mahendra
 
Boolean logic
Boolean logicBoolean logic
Boolean logic
vikram mahendra
 
Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop system
vikram mahendra
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shop
vikram mahendra
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEM
vikram mahendra
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Python
vikram mahendra
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
vikram mahendra
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
vikram mahendra
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
Ad

Recently uploaded (20)

Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
National Information Standards Organization (NISO)
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 

FUNCTIONS IN PYTHON[RANDOM FUNCTION]

  • 1. FUNCTIONS IN PYTHON COMPUTER SCIECNCE(083) CLASS: XII RANDOM FUCTION IN PYTHON PART-2
  • 2. RANDOM MODULE To use random functions import random.py module inside your python program import random Python has a built-in module that you can use to make random numbers. random() randrange() randint() uniform() shuffle() choice()
  • 3. random() This function generates a random float number between 0.0 and 1.0.It include 0, but excluding 1). Example: The random numbers generate by random like: 0.564388123456754 Every time we execute the random() it will generate different floating number. import random print("Printing random number: ”,random.random()) ----OUTPUT---- Printing random number: 0.07252177092679812
  • 4. random() This function generates a random float number between 0.0 and 1.0.It include 0, but excluding 1).So now you know that it generates number between 0.0 to 1.0 ( excluding 1). But if you want the number generate by this random() function between 0 to 5. It means 0 is the lowest number and 4 point something is the highest number.It is due to 0.0 *5 is 0.0 and 1.0 * 5 is 5.0(excluding 5) means 4.0 import random k=random.random()* 5 print(“Random num:”,k) --output--- Random num:0.6715376992415667 --output--- Random num:2.339768894331859 --output--- Random num:1.6150877389220542It generate number from 0.0 to 4.000
  • 5. random() Now you know that it generate number from 0 to less than 5 if we execute above line but in floating value.If we need the result in integer format than, we need to use int() to convert floating value to integer. k=random.random()* 5 import random k=random.random()* 5 print(“Random num:”,int(k)) --output--- Random num: 1 --output--- Random num: 2 --output--- Random num: 4 --output--- Random num: 0 It include lowest number that is by default 0 and exclude the highest number
  • 6. random() Now if we want to generate numbers not from 0 but from 7 to 15 then? k=random.random()* 15 + 7 After +(Plus) sign the value is the lowest means 7 is the lowest value Before +(Plus) sign the value is Highest means random()* 15 is the highest value So number should generate from 7 included and up to 14 (15 excluded) But it will give some different output, that not match with the result we need:
  • 7. --output--- Random num: 12 k=random.random()* 15 + 7 import random print(“Random num:”,int(k)) --output--- Random num: 7 --output--- Random num: 11 --output--- Random num: 21 --output--- Random num: 15 --output--- Random num: 21 --output--- Random num: 19 So why numbers not generated up to 15 it is due to the method that you need to learn.So if we solve the line random()*15 + 7 means numbers highest number is 14 means from 0 to 14 but when we add 7, we get number from 7 to 21.
  • 8. --output--- Random num: 12 k=random.random()* 9 + 7 import random print(“Random num:”,int(k)) --output--- Random num: 15 --output--- Random num: 7 --output--- Random num: 14 Question is how we will get the result, we need that is : From 7 to 15 Lowest number is 7 and how we get highest number will be =HIGH – LOW + 1 =15-7=8+1=9 So now lowest no. is 7 and high no. is 9+7-1=15,random no. from 7 to 15
  • 9. k=random.random()* 111 + 75 import random print(“Random num:”,int(k)) Question : To generate the number from 75 to 185 Lowest number is 75 and how we get highest number will be =HIGH – LOW =185-75 + 1=110+1=111 Question : To generate the number from 99 to 999 Lowest number is 75 and how we get highest number will be =HIGH – LOW + 1=999-99 + 1=901 k=random.random()* 901 + 99 --output--- Random num: 90 print(“Random num:”,int(k)) --output--- Random num: 158
  • 10. randrange() It returns a random number between the given range Syntax random.randrange(start, stop, step) Parameter Values Parameter Description start stop step Optional. An integer specifying at which position to start. Default 0 Required. An integer specifying at which position to end. Optional. An integer specifying the incrementation. Default 1
  • 11. Question : To generate the random number between 0 and 39 using random.range() import random # Random number between 0 and 39 num1 = random.randrange(40) print("Random integer: ", num1) ----Output------ Random integer: 7 import random # Random number between 20 and 39 num2 = random.randrange(20, 40) print("Random integer: ", num2) Question : To generate the random number between 20 and 39 using random.range() ----Output------ Random integer: 34
  • 12. import random # Random number between 25 and 249 divisible by 5 num3 = random.randrange(25, 250, 5) print("Random integer: ",num3) ----Output------- Random integer: 170 Question : To generate the random number between 25 and 249 divisible by 5 using random.range() import random num3 = random.randrange(2, 40, 2) print("Random integer: ", num3) ----Output------- Random integer: 26 Question : To generate the random number between 2 and 40 divisible by 2 using random.range()
  • 13. What if you want to generate a random number of length n? For example, you want to generate a random number of length 3 means (100,999) import random num1 = random.randrange(100, 1000) print("First random number of length 3 is", num1) ----Output------- Random integer: 160 Generate a random negative integer Let’s see how to generate a random negative integer between -15 to -5. import random k = random.randrange(-15, -5) print("Random no:“,k) ----Output------- Random no:
  • 14. Generate a random integer number multiple of n In this example, we will generate a random number between 3 to 66, which is a multiple of 3 like 3, 6, 39, 66. Generate random positive or negative integer from -10 to 10 import random num = random.randrange(-10, 10) print(num) import random num = random.randrange(3,66,3) print(num)
  • 15. random.randint() Syntax: random.randint(start, stop) This function returns a random integer within a range. This function takes two parameters. Both are mandatory. It will generate a random number from the inclusive range. import random print("Random integer from 0 to 9") k = random.randint(0, 9) print("Random no: ", k) ----Output------- Random no:5
  • 16. Random integer from 10 to 100 using random.randint() num2 = random.randint(10, 100) print("Random integer: ", num2) Random integer: 98 Random integer: 15 Random integer 0,1 using random.randint() num2 = random.randint(0, 1) print("Random integer: ", num2) Random integer: 1 Random integer: 0
  • 17. Program to create random numbers list of 10 numbers from 10 to 100 import random no = [] for i in range(0, 10): no.append(random.randint(10, 100)) print("Print list of 10 random numbers") print(no) Print list of 10 random numbers [36, 80, 57, 98, 55, 20, 38, 64, 87, 39] i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 no.append(random.randint(10,100)) no.append(random.randint(10,100)) no.append(random.randint(10,100)) no.append(random.randint(10,100)) no.append(random.randint(10,100)) no.append(random.randint(10,100)) no.append(random.randint(10,100)) no.append(random.randint(10,100)) no.append(random.randint(10,100)) no.append(random.randint(10,100)) [ ]36 ,80,57,98,55,20,38,64,87,39
  • 18. What possible outputs(s) of the following code? Also specify the maximum and minimum values that can be assigned to variable NUM import random NAV=[“LEFT”,”FRONT”,RIGHT”,”BACK”]; NUM=random.randint(1,3) NAVG=“” for C in range(NUM,1,-1): NAVG=NAVG+NAV[C] print (NAVG) (i) BACKRIGHT (ii) BACKRIGHTBACK (iv) BACK (iii) LEFTFRONTRIGHT 0 1 2 3 4 0 L E F T 1 F R O N T 2 R I G H T 3 B A C K NUM 321 CONDITIONS: IF randint() generate 1 then range(1,1,-1) means 0, it does not run IF randint() generate 2 then range(2,1,-1) means 2 only, “RIGHT” IF randint() generate 3 then range(3,1,-1) means 3,2 only, “RIGHT” ,”BACK” 13
  • 19. What possible outputs(s) of the following code? Also specify the maximum and minimum values that can be assigned to variable NUM import random P=“MY PROGRAM” x=0 while(P[x] != ‘R’): a=random.randint(0,3)+5 print(P[a], “-”,end=“”) x=x+1 (i) R-P-O-R- (ii) P-O-R-Y- (iv) A-G-R-M- (iii) O-R-A-G- 0 1 2 3 4 5 6 7 8 9 M Y P R O G R A M MIN: 5 MAX: 8 a random no. starts from 5 to 8 means 5,6,7,8
  • 20. What possible outputs(s) are expected to be displayed on screen at the time of execution of the program from the following code? Also specify the maximum values that can be assigned to each of the variables FROM and TO. import random AR=[20,30,40,50,60,70]; FROM=random.randint(1,3) TO=random.randint(2,4) for K in range(FROM,TO+1): print (AR[K],end=”#“) (i) 10#40#70# (ii) 30#40#50# (iii) 50#60#70# (iv) 40#50#70# FROM random no. 1, 2, 3 TO random no. 2, 3, 4 0 1 2 3 4 5 20 30 40 50 60 70 Now loop starts from FROM(1,2,3) And Ends at TO+1 means if TO is (2,3,4) Inside range second value run less than given number(2 means 1, 3 means 2 and 4 means 3)that why we put TO+1(2,3,4) If FROM random generate no 1 means AR[1]= 30 or 2 means AR[2]=40 or AR[3]=50 If TO random generate no 2 means AR[2]= 40 or 3 means AR[3]=50 or AR[4]=60