0% found this document useful (0 votes)
139 views10 pages

3 Random

The document discusses various functions from the random module in Python for generating pseudorandom numbers and values. It covers randrange, randint, choice, shuffle, and provides examples of using each function.

Uploaded by

maheshdalle59
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)
139 views10 pages

3 Random

The document discusses various functions from the random module in Python for generating pseudorandom numbers and values. It covers randrange, randint, choice, shuffle, and provides examples of using each function.

Uploaded by

maheshdalle59
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
You are on page 1/ 10

DoyPyEdu PLAY WITH PYTHON

RANDOM
6

 RANDRANGE
 RANDINT
Random 

CHOICE
CHOICES
 SHUFFLE
 New Randomize Output Assignment
How to use random.randrange(): There are multiple variants of random.randrange() function let see
how to use it with the help of different examples.
The Syntax of random.randrange() function
random.randrange(start, stop[, step])
random.randrange() function takes three parameters. Out of three parameters, two is optional. i.e., start and
step is the optional parameter.
Note: randrange (start, stop, step) doesn’t consider the last item i.e. it is exclusive. For example, randrange
(10,20,1) will return any random number from 10 to 19 (exclusive). it will never select 20.
Start argument is the starting number in a range. i.e., lower limit. By default starts with 0 if not specified.
Stop argument is the last number in a range. Stop argument is the upper limit.
The step is a difference between each number in the sequence. The step is optional parameters. The default
value of step is 1 if not specified.
It will raise a ValueError if you used values other than an integer. i.e., You cannot use float value parameters
in

Page 1 of 10 EDUCATION FOR EVERYONE


DoyPyEdu PLAY WITH PYTHON
Randrange Functions:
Randrange:Generate a randomly selected element from range(start, stop, step)
random.randrange(start, stop[, step])
import random
for i in range(3):
print random.randrange(0, 101, 5)
It will raise a ValueError if stop <= start or step =0.
random.randrange() Examples
Here in the following example, we are trying to print a random number in a given range. This example
demonstrates all the variants of random.randrange() function.
import random
print("Generate random integer number within a given range in Python ")
#random.randrange() with only one argument
print("Random number between 0 and 10 : ", random.randrange(10)) #0 to 9 any
#random.randrange() with two arguments
print("Random number between 20 and 40 : ", random.randrange(20, 40)) #20 to 39 any
#random.randrange() with three argument
print("Random number between 0 and 60 : ", random.randrange(0, 60, 6)) #0 to 59 any step value 6
Output:
Execute Online
Generate random integer number within a given range in Python
Random number between 0 and 10 : 7
Random number between 20 and 40 : 32
Random number between 0 and 60 : 48

Randint functions:
The Random module contains some very useful functions randint(start, stop, step)
If we wanted a random integer, we can use the randint function Randint accepts two parameters: a lowest and
a highest number. Generate integers between 1,5. The first value should be less than the second.
import random
print random.randint(0, 5)
This will output either 0,1, 2, 3, 4 or 5. #0 to 5 any
If you want a larger number, you can multiply it.
For example, a random number between 0 and 100:
import random
random.random() * 100

Page 2 of 10 EDUCATION FOR EVERYONE


DoyPyEdu PLAY WITH PYTHON

Choice functions: Generate a random value from the sequence sequence. random.choice( ['red', 'black',
'green'] ).The choice function can often be used for choosing a random element from a list.
import random
myList = [2, 109, False, 10, "Lorem", 482, "Ipsum"]
random.choice(myList)

Page 3 of 10 EDUCATION FOR EVERYONE


DoyPyEdu PLAY WITH PYTHON

Shuffle functions::The shuffle function, shuffles the elements in list in place, so they are in a random
order.
random.shuffle(list)
Example taken from this post on Stackoverflow
from random import shuffle
Page 4 of 10 EDUCATION FOR EVERYONE
DoyPyEdu PLAY WITH PYTHON
x = [[i] for i in range(10)]
shuffle(x)
Output:
# print x gives [[9], [2], [7], [0], [4], [5], [3], [1], [8], [6]]
# o:f course your results will vary

Page 5 of 10 EDUCATION FOR EVERYONE


DoyPyEdu PLAY WITH PYTHON
NEW IN PYTHON (RANDOMIZE OUTPUT ASSIGNMENT)
.5 mark for each correct answer upto max. 2 marks.
import random
Que. 1 .
N=20
for i in range(4):
Guessnum=random.randrange(0,N-10)+10
print(Guessnum,end=' ')
# Output Options : #10 to 19 , 4 numbers
Que.2
Guess=65
for I in range(1,5):
New=Guess+random.randrange(0,I)
print(chr(New),end=' ')
# Output Options :
i. A B B C ii. A C B A iii. B C DA iv. C A B D
Que.3. n=1 # iii. 1 2 3 4 5 iv. 1 2 3 4 5 6 7 8
VAL=12 Que.4.
Rnd=8 + random.randrange(0,VAL) * 1 RN=random.randrange(0,4)+5;
while(n<=Rnd): for I in range(1,RN+1):
print(n,end=" ") print(i,end=' ')
n+=1 # Output Options:
# Output Options : i. 0 1 2 ii. 1 2 3 4 5 6 7 8
i. 1 2 3 4 5 6 7 8 9 10 11 12 13 ii. 0 1 2 3 iii. 4 5 6 7 8 9 iv. 5 6 7 8 9 10 11 12
Que.5.
Score=[ 25,20,34,56, 72, 63]
Myscore = Score[2 + random.randrange(0,2)]
print(Myscore)
# Output Options :
i. 25 ii. 34 iii. 20 iv. None of the above
Que.6.
Marks = [99, 92, 94, 96, 93, 95]
MyMarks = Marks [1 + random.randrange(0,2) ]
print(MyMarks)
# Output Options :
i. 99 ii. 94 iii. 96 iv. None of the above
Que.7. Fly = random.randrange(0,2) + 1 ;
Disp=22 print( city[Fly] ,end=":")
Rnd=random.randrange(0,Disp)+15 #Output Options:
N=1 i. DEL: CHN : KOL : ii. CHN : KOL : CHN :
for I in range(3,Rnd,4): iii. KOL : BOM : BNG : iv. KOL : CHN : KOL :
print(N,end=" ") Que.10.
N+=1 Area=["NORTH","SOUTH","EAST","WEST"]
#Output Options: for I in range(3):
i. 1 ii. 1 2 3 4 iii. 1 2 iv. 1 2 3 ToGo=random.randrange(0,2) + 1
Que.8. print(Area[ToGo],end=":")
N=37 #Output Options:
Guess_num=random.randrange(0,N-8)+38 i. SOUTH : EAST : SOUTH :ii. NORTH : SOUTH : EAST :
iii. SOUTH : EAST : WEST : iv. SOUTH : EAST : EAST :
print(Guess_num)
#what maximum and minimum values the program Que.11. Num=13
could possibly display? RndNum = random.randrange(0,Num) +13;
for N in range(1,RndNum):
Que.9.
print(N,end=" ")
city= [ "DEL", "CHN", "KOL", "BOM", "BNG"]
for I in range(1,4): Que.12.
Page 6 of 10 EDUCATION FOR EVERYONE
DoyPyEdu PLAY WITH PYTHON
r=random.randrange(0,20)+random.randrange(0,2) MAX = 3;
print(r) digit =80 + random.randrange(0,MAX )
Que.13.#fill in the blanks for the statement1 with the for R in range (digit,80-1,-1):
help of random function print(R,end="$")
#if the number generated by the random number is #Output Options:
supposed to be between the range of 25-2025. i. 83$82$81$80$ ii. 80$81$82$ iii. 80$81$ iv. 81$80$
#r=_______________//statement 1 Que.21.
print(r) p , q = 99,999
Que.14. x=random.randrange(0,3)+4
#fill in the blanks for the statement1 with the help of y=random.randrange(0,2)+2
random function for i in range(x):
#if the number generated by the random number is print("#",end="")
supposed to be between the range of 20-2000. print(p,"-",end="")
#r=_______________//statement 1 for i in range(x):
print(r) print("@",end="")
Que.15 print(q)
#fill in the blanks for the statement1 with the help of #Output Options:
random function i. ##99-@999 ii. ##99-@@999
#if the number generated by the random number is iii. ######99-@@999 iv. ####99-@@@
supposed to be between the range of 41to 441. Que.22.
#r=_______________//statement 1 LIMIT = 4
print(r) Points = 100 +random.randrange(0,LIMIT);
Que.16 for p in range(Points,101,-1):
Low,point=25,5 print(p,end="#")
for i in range (1,5): #Output Options:
Number=low + random.randrange(0,point); i. 103#102#101#100# ii. 100#101#102#103#
iii. 100#101#102#103#104# iv. 104#103#102#101#100#
print(Number,end="")
point-=1; Que.23.
#Output Options: #if the value of N given by the user is 20,
i. 29: 26:25 :28 : ii. 24: 28:25 :26 : #what maximum and minimum values the program
iii. 29: 26:24 :28 : iv. 29: 26:25 :26 : could possibly display?
N=20
Que.17
Guessnum=random.randrange(0,N-10)+10;
Max=3
print(Guessnum)
Div=1+random.randrange(0,Max)
for N in range(1,5): Que.24.
print(100%Div,end="#") High=4
#Output Options: Guess=random.randrange(0,High) +50
i. 0#0#0#0# ii. 1#1#1#1# iii. 2#2#2#2# iv. 3#3#3#3# for C in range(Guess, 56):
print(C,end="#")
Que.18
#Output Options:
MIN, SCORE = 25,10
i 50 # 51 # 52 # 53 # 54 # 55 # ii 52 # 53 # 54 # 55 #
for i in range (1,5):
iii 53 # 54 # iv 51 # 52 # 53 # 54 # 55
Num = MIN + random.randrange(0,SCORE)
print(Num,end=":") Que.25.
SCORE-=1; LOW , POINT =15 ,5
#Output Options: for I in range (1,5):
i. 34:31:30:33: ii. 29:33:30:31: Number = LOW + random.randrange(0,POINT)
iii. 34:31:30:31: iv. 34:31:29:33: print(Number ,end=":")
POINT-=1
Que.19
#Output Options:
Max=3;
i.19:16:15:18: (ii. 14:18: 15:16:
Number=50+random.randrange(0,Max)
iii. 19:16:14:18: (iv. 19:16:15:16:
for P in range(Number,50+1,-1):
print(P,end="#") Que.26.
#Output Options: Max=4;
i. 53#52#51#50# ii. 50#51#52# iii. 50#51# iv. 51#50# Mynum=20+random.randrange(0,Max);
for N in range (Mynum,26):
Que.20
Page 7 of 10 EDUCATION FOR EVERYONE
DoyPyEdu PLAY WITH PYTHON
print(N,end="*") for i in range ( 4):
#Output Options: print(number[2+ random.randrange(0,2) - 1 ],end="")
i. 20*21*22*23*24*25* ii. 22*23*24*25* #Output Options:
iii. 23*24* iv. 21*22*23*24*25 i. The winner is : A2776 ii. The winner is : D6766
Que. 27. #iii. The winner is : B6767 iv. The winner is : C3672
Score, n=[25,20,34,56, 72, 63], 2 Que.31.
Myscore = Score[1 + random.randrange(0,n)]+4; Arr=[ 9,6]
print(Myscore) Chance=random.randrange(0,2)+10
#Output Options: for I in range (2):
i. 25 ii. 72 iii. 24 iv. None of the above N=random.randrange(0,2)
Que.28. print(Arr[N]+Chance,end="*")
serial=['E', 'X', 'A', 'M']; number=[ 69, 66, 67, 68] #Output Options:
print(number[random.randrange(0,3)]) i. 9*6* ii. 19*17* iii. 19*16* iv. 20*16*
for i in range (4): Que. 32. A ="WELCOME"; ToGo=2
print(serial[2+ random.randrange(0,2) - 1 ]) for I in range (len(A)):
#Output Options: ToGo=random.randrange(0,2*2) +1;
i. 66AAXA ii. 67AAAM iii. 67XXAX iv. 69AXXA print(A[ToGo],end=":")
Que.29. #Output Options:
p=["Computer","Mouse","Keyboard","Pen Drive"] i. W: E: L: C: O: M: E: ii. E: C: E: E: C: C: E:#
for i in range (1, 4): #iii.E: C: E: E: C: C: O: iv. C: C: C: E: E: C: C:
print(p[random.randrange(i)],end=" ") Que.33. X =[100,75,10,125]
#Output Options: Go =random.randrange(0,2)+2
# i. Mouse Keyboard Pen drive ii. Computer Computer for i in range (Go,4):
Keyboard print(X[i],end="$$")
iii. Computer Computer Mouse iv. None of the above
#Output Options:
Que.30. i. 100$$75 ii. 75$$10$$125$$
serial=['A', 'B', 'C', 'D']; number=[2, 6, 7, 3 ] iii. 75$$10$$ iv.10$$125$$
print(" The DATA is : ",end="")
print(serial [random.randrange(0,3)],end="")
OUTPUT
1. 10,19 10. i,iv 19. iv 28. i, iii, iv
2. i 11. 1 to 13 between 25 20. iv 29. ii, iii
3. iv 12. 0,20 21. iii 30. iii
4. ii 13. random.randrange (25,2026) 22. i 31. ii
5. ii 14. random.randrange (20,2001) 23. 19 , 10 32. iii, iv
6. ii 15. random.randrange (41,442) 24. i, ii 33. iv
7. ii 16. iv 25. d
8. 38,66 17. i,ii 26. a, b
9. ii,iv 18. ii,iii 27. iii
Unsolved Problems
Q 1. What possible outputs(s) are expected to be import random
displayed on screen at the time ofexecution of the PICK = random.randint(0,3)
program from the following code ? Also specify the CITY= [“Durgapur”, “Asansol”, “Bokaro”,”Dhanbad”]
maximum values that can be assigned to each of the for x in CITY:
variables START and END. for y in range(1, PICK):
import random print (x, end = “ “)
SCORE=[20,40,10,30,15] print()
START=random.randint(1,3) (i) Durgapur Durgapur
END=random.randint(2,4) Asansol Asansol
for I in range(START,END+1): Bokaro Bokaro
print (VALUES[I],end="&") Dhanbad Dhanbad
(i) 10&40&20& (ii) 10&30&15& (ii) Durgapur
(iii) 40&10&30& (iv) 20&40&210& Asansol
Q 2. What are the possible output(s) of the following Bokaro
code? Also specify the maximum and minimum values Dhanbad
that can be assigned to variable PICK. (iii) Durgapur
Page 8 of 10 EDUCATION FOR EVERYONE
DoyPyEdu PLAY WITH PYTHON
Durgapur Asansol (b) Find out, which line of output(s) out of (i) to (iv)
Durgapur Asansol Bokaro will not be expected from the program?
Durgapur Asansol Bokaro Dhanbad i. 0#1 ii. 1#2 iii. 2#3 iv. 3#4
(iv) Durgapur Q 7. Find and write the output of the following
Asansol Asansol Python code:
Bokaro Bokaro Bokaro Data = ["P",20,"R",10,"S",30,"T",50]
Q 3. What possible output(s) are expected to be Times, Alpha,Add = 0, "" , 0
displayed on screen at the time of execution of the for C in range(1,8,2):
program from the following code? Also specify the Times= Times + C
minimum values that can be assigned to each of the Alpha= Alpha + Data[C-1]+"$"
variables BEGIN and LAST. Add = Add + Data[C]
import random print(Times,Add,Alpha)
VALUES = [10, 20, 30, 40, 50, 60, 70, 80] a.16 110 P$R$S$T$ b. 9 60 P$R$S$
BEGIN = random.randint (1, 3) c. 6 50 P$S$ d. all
LAST = random.randint(2, 4) Q 8. What possible outputs(s) are expected to be
for I in range (BEGIN, LAST+1): displayed on screen at the time of execution of the
print (VALUES[I], end = "-") program from the following code? Also specify the
(i) 30-40-50- (ii) 10-20-30-40- maximum values that can be assigned to each of the
(iii) 30-40-50-60- (iv) 30-40-50-60-70- variables FROM and TO.
Q 4. c) What are the possible outcome(s) executed import random
from the following code? Also specify the maximum AR=[20,30,40,50,60,70];
and minimum values that can be assigned to variable FROM=random.randint(1,3)
NUMBER. Justify. TO=random.randint(2,4)
import random for K in range(FROM,TO+1):
STRING="CBSEONLINE" print (AR[K],end=”# “)
NUMBER=random.randint(0,3) (i) 10#40#70# (ii) 30#40#50#
N=9 (iii) 50#60#70# (iv) 40#50#70#
while STRING[N] !='L': #CBSEONINE Ans. 30#40#50# Maximum value FROM,TO is 3,4)
print (STRING[N]+STRING[NUMBER]+'#') Q 9. What possible outputs(s) are expected to be
NUMBER=NUMBER+l displayed on screen at the time of execution of the
N=N-l program from the following code? Also specify the
(i) ES#NE#IO# (ii) LE#NO#ON# maximum values that can be assigned to each of the
(iii) NS#IE#LO# (iv) EC#NB#IS# variables Lower and Upper.
Q 5. What possible output(s) are expected to be import random
displayed on screen at the time of execution of the AR=[20,30,40,50,60,70];
program from the following code? Also specify the Lower =random.randint(1,3)
maximum values that can be assigned to each of the Upper =random.randint(2,4)
variables HIGH and LOW? for K in range(Lower, Upper +1):
import random print (AR[K],end=”#“)
AR=[22,33,44,55,66,77] (i) 10#40#70# (ii) 30#40#50#
LOW=random.randint(1,3) (iii) 50#60#70# (iv) 40#50#70#
HIGH=random.randint(2,4) Q 10.What possible outputs(s) are expected to be
for p in range(LOW,HIGH + 1): displayed on screen at the time of execution of the
print(AR[p],end=’*’) program from the following code? Also specify the
(i)11*22*33* (ii) 33*44*55* minimum value that can be assigned to BEGIN and
(iii) 55*66*77* (iv) 44*55*66* maximum value that can be assigned to LAST.
Q 6. Observe the following program and answer the import random
questions that follow: POINTS=[20,40,10,30,50];
import random BEGIN=random.randint(0,3)
X=3 LAST=random.randint(2,3)
N=random.randint(1,X) for C in range(BEGIN,LAST+1):
for i in range(N): print (POINTS[C],"#",end="")
print(i,'#',i+1) (i) 20 #40 # (ii) 50 #20 #40 #
(a) What is the minimum and maximum number N & (iii) 30 #50 #20 # (iv) 50 #20 #40 #50 #
How many times the loop will execute?

Page 9 of 10 EDUCATION FOR EVERYONE


DoyPyEdu PLAY WITH PYTHON

OUTPUT

1. (iii) 40&10&30& b. 0#1


2. i, ii, iv 7. a. 16 110 P$R$S$T$
3. (i) 30-40-50- 8. (ii) 30#40#50#
4. (iv) EC#NB#IS# 9. (ii) 30#40#50#
5. (ii)33*44*55* 10. (i) 20 #40 #
(iv)44*55*66*
6. a. N minimum 1, N maximum 3
How many times the loop will execute? : 3

**********************************

Page 10 of 10 EDUCATION FOR EVERYONE

You might also like