0% found this document useful (0 votes)
24 views

Functions2 Python

The possible outputs are: LEFT FRONT RIGHT The minimum value that can be assigned to variable NUM is 1. The maximum value is 3. NUM randomly picks a number between 1 and 3 (inclusive). This number is used as the upper limit in the range() function. range(NUM,1,-1) counts down from NUM to 1 with a step of -1. Each time through the loop, it picks one element from the NAV list and appends it to the string NAVG. Since the list only has 4 elements, the maximum output length is 4.

Uploaded by

kpaijit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Functions2 Python

The possible outputs are: LEFT FRONT RIGHT The minimum value that can be assigned to variable NUM is 1. The maximum value is 3. NUM randomly picks a number between 1 and 3 (inclusive). This number is used as the upper limit in the range() function. range(NUM,1,-1) counts down from NUM to 1 with a step of -1. Each time through the loop, it picks one element from the NAV list and appends it to the string NAVG. Since the list only has 4 elements, the maximum output length is 4.

Uploaded by

kpaijit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

FUNCTIONS IN PYTHON

RANDOM FUCTION IN
PYTHON

COMPUTER SCIECNCE(083)
CLASS: XII 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 --output---
Random num:0.6715376992415667
k=random.random()* 5
--output---
print(“Random num:”,k) Random num:2.339768894331859
--output---
It generate number from 0.0 to 4.000 Random num:1.6150877389220542
random() k=random.random()* 5

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.

import random --output---


k=random.random()* 5 Random num: 1
print(“Random num:”,int(k)) --output---
Random num: 2
--output---
It include lowest number Random num: 4
that is by default 0 and
--output---
exclude the highest number
Random num: 0
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


Before +(Plus) sign the value is
is the lowest means 7 is the
Highest means random()* 15 is
lowest value
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:
import random So why numbers not generated up to 15 it is due
to the method that you need to learn.So if we
k=random.random()* 15 + 7
solve the line random()*15 + 7 means numbers
print(“Random num:”,int(k)) 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
--output---
--output--- Random num: 15
--output---
Random num: 7 Random num: 19
--output---
--output---
Random num: 21
Random num: 11
--output---
Random num: 21
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
import random So now lowest no. is 7 and high
k=random.random()* 9 + 7 no. is 9+7-1=15,random no.
print(“Random num:”,int(k)) from 7 to 15

--output--- --output---
Random num: 12 Random num: 7

--output--- --output---
Random num: 15 Random num: 14
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
import random
--output---
k=random.random()* 111 + 75 Random num: 90
print(“Random num:”,int(k))

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---


print(“Random num:”,int(k)) Random num: 158
randrange() It returns a random number between the given range

Syntax random.randrange(start, stop, step)

Parameter Values

Parameter Description

start Optional. An integer specifying at which position to start.


Default 0

stop Required. An integer specifying at which position to end.

Optional. An integer specifying the incrementation.


step
Default 1
Question : To generate the random number between 0 and 39 using random.range()

import random
# Random number between 0 and 39 ----Output------
num1 = random.randrange(40) Random integer: 7
print("Random integer: ", num1)

Question : To generate the random number between 20 and 39 using random.range()

import random
# Random number between 20 and 39
num2 = random.randrange(20, 40) ----Output------
print("Random integer: ", num2) Random integer: 34
Question : To generate the random number between 25 and 249 divisible by 5
using random.range()

import random
# Random number between 25 and 249 divisible by 5
num3 = random.randrange(25, 250, 5) ----Output-------
print("Random integer: ",num3) Random integer: 170
Question : To generate the random number between 2 and 40 divisible by 2
using random.range()

import random
num3 = random.randrange(2, 40, 2) ----Output-------
print("Random integer: ", num3) Random integer: 26
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) ----Output-------
print("Random no:“,k) 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.

import random
num = random.randrange(3,66,3)
print(num)

Generate random positive or negative integer from -10 to 10

import random
num = random.randrange(-10, 10)
print(num)
random.randint()
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.

Syntax:
random.randint(start, stop)

import random
print("Random integer from 0 to 9")
k = random.randint(0, 9) ----Output-------
print("Random no: ", k) 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 i=0 no.append(random.randint(10,100))
no = [] i=1 no.append(random.randint(10,100))
for i in range(0, 10):
i=2 no.append(random.randint(10,100))
no.append(random.randint(10, 100))
print("Print list of 10 random numbers") i=3 no.append(random.randint(10,100))
print(no) i=4 no.append(random.randint(10,100))
i=5 no.append(random.randint(10,100))
[ 36 ,80,57,98,55,20,38,64,87,39 ]
i=6 no.append(random.randint(10,100))
i=7 no.append(random.randint(10,100))
Print list of 10 random numbers i=8 no.append(random.randint(10,100))
[36, 80, 57, 98, 55, 20, 38, 64, 87, 39] i=9 no.append(random.randint(10,100))
What possible outputs(s) of the following code? Also specify the maximum and minimum
values that can be assigned to variable NUM 3 1
0 1 2 3 4
import random 1 2 3 0 L E F T
NAV=[“LEFT”,”FRONT”,RIGHT”,”BACK”]; 1 F R O T N
NUM=random.randint(1,3) NUM 2 R I G T H
NAVG=“” 3 B A C K
for C in range(NUM,1,-1):
CONDITIONS:
NAVG=NAVG+NAV[C]
print (NAVG) IF randint() generate 1 then range(1,1,-1)
means 0, it does not run
(i) BACKRIGHT (ii) BACKRIGHTBACK
IF randint() generate 2 then range(2,1,-1)
means 2 only, “RIGHT”
(iii) LEFTFRONTRIGHT
IF randint() generate 3 then range(3,1,-1)
(iv) BACK means 3,2 only, “RIGHT” ,”BACK”
What possible outputs(s) of the following code? Also specify the maximum and minimum
values that can be assigned to variable NUM

import random a random no. starts from 5 to 8 means 5,6,7,8


P=“MY PROGRAM”
x=0 0 1 2 3 4 5 6 7 8 9
while(P[x] != ‘R’):
M Y P R O G R A M
a=random.randint(0,3)+5
print(P[a], “-”,end=“”)
x=x+1
MIN: 5 MAX: 8
(i) R-P-O-R- (ii) P-O-R-Y-

(iii) O-R-A-G-

(iv) A-G-R-M-
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 (i) 10#40#70# (iii) 50#60#70#
AR=[20,30,40,50,60,70]; (ii) 30#40#50# (iv) 40#50#70#
FROM=random.randint(1,3)
Now loop starts from FROM(1,2,3)
TO=random.randint(2,4) And Ends at TO+1 means if TO is (2,3,4)
for K in range(FROM,TO+1): Inside range second value run less than given
print (AR[K],end=”#“) number(2 means 1, 3 means 2 and 4 means
3)that why we put TO+1(2,3,4)
FROM random no. 1, 2, 3
TO random no. 2, 3, 4 If FROM random generate no 1 means AR[1]=
30 or 2 means AR[2]=40 or AR[3]=50
0 1 2 3 4 5 If TO random generate no 2 means AR[2]= 40 or
20 30 40 50 60 70 3 means AR[3]=50 or AR[4]=60

You might also like