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

Random Module

The document provides an overview of the Random Module in Python, detailing its functions such as random(), randrange(), randint(), uniform(), choice(), and shuffle(). It includes syntax examples and explanations of how these functions can generate random numbers, select random elements from sequences, and shuffle lists. Additionally, it poses questions for practice related to the use of these functions in coding scenarios.

Uploaded by

Prathish G 8 A
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)
20 views4 pages

Random Module

The document provides an overview of the Random Module in Python, detailing its functions such as random(), randrange(), randint(), uniform(), choice(), and shuffle(). It includes syntax examples and explanations of how these functions can generate random numbers, select random elements from sequences, and shuffle lists. Additionally, it poses questions for practice related to the use of these functions in coding scenarios.

Uploaded by

Prathish G 8 A
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

[Link] Sansakriti Sen.

Secondary School,Vyaspur Distt-Yamunanagar


RANDOM MODULE

[Link](2,4)

Random Module:

It is a build-in module in python 5. choice() :

It will use generate the random numbers. This method is used for random selection from the
list,tuple,string and any other sequence.
It can choose the random element from the list.
Syntax:
It can reshuffle the string.
[Link](<sequence>)
It can also generate the floating point numbers.
Example:L=[11,22,33,44]
Functions of Randon Module:
[Link](L)
[Link]():
6. shuffle() :
• This will generate floating values between 0
and 1 (including 0 but excluding 1).It does This method can shuffle the item of a given list.
not require any argument.
Shuffle list x in place and return None.
Synatx: [Link]()
Syntax:
[Link]():
[Link](<sequence>)
• This method always return an integer
Example:
between given lower and upper limit
(excluding upper limit). L=[14,23,45,67]
Syntax: [Link](L)
[Link](20)

#It will generate random integers between 0,19

[Link](0,6,2)

# It will generate random value 0,2,4

[Link]() :

This method takes two arguments a,b in which first is


lower limit and second one is upper limit.

This function will return any integer number between


the two number including both limits.

[Link](1,6)

# It will generate random value 1,2,3,4,5,6

4. uniform() :

This method return any floating point number


between two given numbers.

Get a random number in the range [a,b] or [a,b]


depending or rounding
1
Prepared By:Ravinder Chauhan
[Link] Sansakriti [Link] School,Vyaspur Distt-Yamunanagar
RANDOM MODULE

b can be anything between 1 to 6


Questions:
let b =6
Identify the correct output of the following [Link] for I in range(0,6,2)
write the Minimum and Maximum possible values of it will print W#s#o#
the variable b.(SQP 2024)
Q2.
import random import random
a=“Wisdom” X=[Link]()
Y=[Link](0,4)
b=[Link](1,6) print(int(X), “:” ,Y+int(X)) (SQP-2017)
for i in range(0,b,2):
a)0:0 b)1:6 c) 2:4 d) 2:3
print(a[i],end=“#”)
Ans: Option a)

a)W# b) W#i# c) W#s# d)W#i#s# Solution:

Ans: option a,c


X=[Link]()
Solution: Here random() function will generate the values
between 0,1 excluding 1 so values may be
0 1 2 3 4 5 0,0.1,0.12……….so on.
W i s d o m Y=[Link](0,4)
Value of Y will be 0,1,2,3,4
b can be anything between 1 to 6 print(int(X), “:” ,Y+int(X))
(Minimum=1,Maximum=6) here int(X) is always generate integer value that will be
let b =1 0 here
for I in range(0,1,2) so possible outputs will be
it will print W# 0:0 ,0:1 , 0:2 ,0:3 ,0:4
b can be anything between 1 to 6
let b =2 Q3.
for I in range(0,2,2) Import random
it will print nothing Values=[10,20,30,40,50,60,70,80]
BEGIN=[Link](1,3)
b can be anything between 1 to 6 LAST=[Link](2,4)
let b =3 for i in range(BEGIN,LAST+1):
for I in range(0,3,2) print(Value[i],end=”-”)
it will print W#s#
a) 30-40-50- c) 10-20-30-40-
b can be anything between 1 to 6 b) 30-40-50-60- d) 30-40-50-60-70-
let b =4
for I in range(0,4,2) Ans: option a)
it will print W#s# Min and Max value of BEGIN= 1,3
Min and Max value of LAST= 2,4
b can be anything between 1 to 6
let b =5 Solution:
for I in range(0,5,2) Value of BEGIN=1,2,3 Value of LAST=2,3,4
it will print W#s#o# Let assume BEGIN=1 and LAST=4
2
Prepared By:Ravinder Chauhan
[Link] Sansakriti [Link] School,Vyaspur Distt-Yamunanagar
RANDOM MODULE

for i in range(1, 4+1) if pick=3 loop will execute 2 times


inside the loop print(I,end=””) will print
so possible output will start from index 1 and end at DELHIDELHI
index 4 MUMBAIMUMBAI
so output will be in between 20-30-40-50- CHANNAICHANNAI
KOLKATAKOLKATA

Q4: Questions for Practice:


import random What will be the expected output after execute the
pick=[Link](0,3) following codes and write the maximum and minimum
city=["DELHI","MUMBAI","CHENNAI","KOLKATA"] value for the assigned variable of randint() function
for I in city:
for j in range(1,pick):
print(I,end="") (NOTE:More then one option can be correct in random
print() module based Question )

a)DELHI b)DELHI 1.
MUMBAI DELHIMUMBAI import random
CHENNAI DELHIMUMBAICHANNAI a="programming"
KOLKATA b=[Link](2,8)
for i in range(b):
c) d) DELHI if i%2==0:
DELHIDELHI MUMBAIMUMBAI print(a[i],end="*")
MUMBAIMUMBAI CHANNAICHANNAI
CHANNAICHANNAI KOLKATA a) p*o*r*m* b) p*o c)prg* d)pog*
KOLKATAKOLKATA
2.
Ans : Option a),c) import random
Min pick is 0 A=[10,30,40,50,60,70]
Max pick is 3 From=[Link](1,3)
To=[Link](2,4)
0 1 2 3 for k in range(From,To+1):
DELHI MUMBAI CHANNAI KOLKATA print(A[k],end="#")
Solution
a) 10#40#70# b) 30#40#50#
for I in city: c)50#60#70 d)40#50#70
this loop have the value of I
DELHI,MUMBAI,CHANNAI,KOLKATA 3.
import random
for j in range(1,pick): A=["EAST","WEST","NORTH","SOUTH"]
if pick=0 loop not execute N=[Link](1,3)
if pick=1 loop not execute out=""
if pick=2 loop will execute one time for i in range(N,1,-1):
inside the loop print(I,end=””) will print out=out+A[i]
DELHI print(out) (SQP 2018)
MUMBAI
CHANNAI a)SOUTHNORTH b)SOUTHNORTHWEST
KOLKATA c)SOUTH d)EASTWESTNORTH
3
Prepared By:Ravinder Chauhan
[Link] Sansakriti [Link] School,Vyaspur Distt-Yamunanagar
RANDOM MODULE

8.
import random
4.
P=[Link](1,3)
import random Colour=[“yellow”,”purple”,”orange”,”black”]
a="Exploration" for I in Colour:
b=[Link](1,6) for j in range(1,P):
for i in range(b,len(a)): print(I,end=””)
if i%2==0: print()
print(a[i],end="@")
a)E@p@o@a@i@n@ b)l@r@t@o@n@ a) yellow c) yellowyellow
c)x@l@r@t@o@n@ d)p@o@a@i@n@ purple purplepurple
orange orangeorange
black blackblack
5.

import random b) yellowyellowyellow d)yellow


NAV=["LEFT","FRONT","RIGHT","BACK"] purplepurple orangeorange
NUM=[Link](1,3) orangeorangeorange purplepurple
NAVG="" blackblack black
for C in range(NUM,1,-1):
NAVG=NAVG+NAV[C]
print(NAVG)

a)BACKRIGHT b) BACKRIGHTFRONT

c) BACK d) LEFTFRONTRIGHT

6.

import random
points=[20,40,10,30,15]
points=[30,50,20,40,45]
Begin=[Link](1,3)
last=[Link](2,4)
for i in range(Begin,last+1):
print(points[c],"#")

a)BACKRIGHT b) BACKRIGHTFRONT
c) BACK d) LEFTFRONTRIGHT

7.

import random
points=[20,40,10,30,15]
points=[30,50,20,40,45]
Begin=[Link](1,3)
last=[Link](2,4)
for i in range(Begin,last+1):
print(points[i],end="#")
a) 20#50#30 b) 20#40#45 c) 50#20#40 d) 30#50#20

4
Prepared By:Ravinder Chauhan

Common questions

Powered by AI

Scripts that leverage the random module yield different outcomes based on runtime-generated values. For instance, functions like 'random.randint()' produce non-deterministic results within specified ranges, leading to multiple correct outputs contingent on these randomly chosen parameters. Thus, in the context of random number generation, the inherent variability means several expected results align with the randomness, validating different output scenarios depending on the seed or current state of the random number generator .

Executing the code snippet with 'random.randint(1,3)' for 'BEGIN' and 'random.randint(2,4)' for 'LAST' will result in the loop iterating over a subset of indices from 1 to 4. The possible values for 'BEGIN' are 1, 2, or 3, and for 'LAST' are 2, 3, or 4. The loop iterates from index 'BEGIN' to 'LAST' inclusive, printing values from the list ranging from those indices. For instance, if 'BEGIN=1' and 'LAST=4', it prints the elements at indices 1 through 4 inclusive. This outcome hinges on the randomness in selecting 'BEGIN' and 'LAST', impacting the subset of indices iterated over .

Using 'int(X)' in a print statement, where X is generated by 'random.random()', serves to truncate the floating-point number X to 0, as the 'random()' function only yields numbers between 0.0 and less than 1.0. Thus, when X is cast to an integer, it always becomes 0. The utility lies in ensuring whole number computations or format compliance in subsequent calculations or outputs .

'random.shuffle()' operates by rearranging elements of a list in place, thus its memory usage is minimal as it does not require additional space for a new list. However, in very large datasets, the time complexity of O(n) as elements are swapped in place means execution time increases linearly with the size of the dataset. Efficient memory use is balanced by potential increases in runtime, particularly if the shuffling needs are called frequently on large scales. Hence, careful consideration of computational resources is crucial in performance-critical applications .

The Python random module offers several methods for generating random numbers and manipulating sequences. The 'random()' method generates random floating-point numbers between 0.0 and 1.0 (excluding 1). The 'randrange()' method returns a randomly selected element from a specified range, excluding the upper limit . The 'randint()' method generates a random integer between two specified integers, inclusive of both ends . 'uniform()' gives a floating-point number between two specified numbers . The 'choice()' method selects a random element from a non-empty sequence like a list, tuple, or string . Lastly, 'shuffle()' rearranges the elements of a list in place .

The 'randint()' function generates a random integer between the two specified integers 'a' and 'b', inclusive. This means it can return any integer starting from 'a' and up to 'b', including both limits. For example, if 'randint(1,6)' is called, the possible return values are 1, 2, 3, 4, 5, or 6 .

'random.uniform()' allows specifying any desired range [a,b] or [a,b), thus directly selecting floating-point numbers within these bounds efficiently. In contrast, 'random.random()' alone generates numbers only between 0.0 and less than 1.0, necessitating manual scaling and offsetting to achieve the desired range. This introduces additional steps and potential room for error, whereas 'random.uniform()' directly supports custom ranges with cleaner and more intuitive syntax, enhancing readability and reducing implementation complexity .

The loop iterates over each list item a number of times determined by 'pick', a randomly selected integer using 'random.randint(0,3)'. If 'pick' equals 0 or 1, the inner loop does not run, therefore each item prints only once. If 'pick' is 2, each item prints twice, and for 'pick' equaling 3, each item prints thrice. This behavior illustrates how random selection directly influences the repetition count of each element, controlling the total number of prints within nested loops .

When using 'random.randint(1,6)', the minimum possible value for 'b' is 1 and the maximum value is 6. This function includes both endpoints 1 and 6, providing any integer within this range .

Using 'random.choice()' on a tuple compared to a list results in identical logical operation—a random element is selected from the sequence. However, tuples are immutable, meaning their original order remains unchanged post-operation, while lists can be modified post-selection if needed. This makes tuples suitable for maintaining data integrity when modifying the sequence isn’t required or desired. The choice function remains agnostic to sequence type regarding complexity or randomness distribution, treating all sequence types uniformly while preserving the original data structure for tuples .

You might also like