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

Random-Function-Unveiling-the-Power-of-Randomness-in-Programming.ppt

its a ppt for random functions in python

Uploaded by

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

Random-Function-Unveiling-the-Power-of-Randomness-in-Programming.ppt

its a ppt for random functions in python

Uploaded by

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

Random Function:

Unveiling the Power of


Randomness in
Programming
This presentation will explore the random function in
programming, its uses, and key functionalities.
Introduction to the Random Function
1 Unpredictable Outputs 2 Common Uses
The random function generates Commonly used in simulations, games, and
unpredictable outputs. testing.
Key Features of the Random Function
Random Numbers and Values Variety of Functions

Produces random numbers or values. Offers different functions for specific use cases.
The Random Module in
Python
To use random functions in Python, import the random
module:

import random
Overview of Random Functions
Function Description Example

random.random() Generates a random float random.random() → 0.572


between 0 and 1 (exclusive)

random.randint(a, b) Returns a random integer random.randint(1, 10) → 7


between a and b (inclusive)

random.uniform(a, b) Returns a random float random.uniform(5.5, 9.5) → 6.3


between a and b

random.choice(seq) Selects a random element random.choice(['red', 'blue',


from a sequence 'green']) → 'blue'

random.shuffle(lst) Randomly rearranges random.shuffle([1, 2, 3]) → [3, 1, 2]


elements in a list
Applications of Random
Functions
Games Simulations
Shuffling cards, Weather predictions,
randomizing moves. modeling real-world
scenarios.

Cryptography Testing
Random key generation Generating random test
for secure data for software
communication. validation.
Example Program
import random
print(random.randint(1, 6)) # Simulates a dice
roll
print(random.choice(['head', 'tail'])) #
Simulates a coin toss
Q 1: What will random.randint(1, 5) return
(A) (B)
Any integer between 0 and 5. Any integer between 1 and 4.

(C) (D)
Any integer between 1 and 5. Any integer between 0 and 4.

Correct Answer: (C)


The random.randint(a, b) function returns a random integer between a and b, inclusive. Therefore,
random.randint(1, 5) will return any integer from 1 to 5 (1, 2, 3, 4, or 5).
Q 2:What will the following code output?
import random print(random.choice(['apple', 'banana', 'cherry']))

(A) apple (B) banana

(C) cherry (D) Any one of the above

Correct Answer: (D)


The choice() function randomly selects one element from the list, so the output can be either apple,
banana, or cherry.
Q 3: Which function generates a random
float between two numbers, a and b?
(A) (B)
random() randint(a, b)

(C) (D)
uniform(a, b) shuffle()

Correct Answer: (C)


The uniform(a, b) function generates a random float within the range [a,b][a,b], including the boundaries.
Q 4: What will random.shuffle() do to a list
(A) (B)
Sort the list in ascending order. Sort the list in descending order.

(C) (D)
Rearrange the list in random order. Remove duplicates from the list

Correct Answer: (C)


The shuffle() function rearranges the elements of the list randomly. It modifies the list in place.
Q 5:What will random.random() return?
(A) (B)
A random float between 0 and 1. A random integer between 0 and 1.

(C) (D)
A random float between 1 and 10. A random integer between 1 and 10.

Correct Answer: (A)


The random() function always generates a float between 0 (inclusive) and 1 (exclusive).
Thank You

You might also like