0% found this document useful (0 votes)
9 views9 pages

Python Math and Random Functions Guide

The document provides an overview of various mathematical and random functions available in Python's math and random modules. It includes examples of using functions such as sqrt, ceil, floor, fmod, and random number generation methods like random(), uniform(), randint(), randrange(), and choice(). Additionally, it demonstrates how to calculate the area of a circle using user input for the radius.
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)
9 views9 pages

Python Math and Random Functions Guide

The document provides an overview of various mathematical and random functions available in Python's math and random modules. It includes examples of using functions such as sqrt, ceil, floor, fmod, and random number generation methods like random(), uniform(), randint(), randrange(), and choice(). Additionally, it demonstrates how to calculate the area of a circle using user input for the radius.
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

11-11-2023

Saturday, November 11, 2023 7:03 PM

sqrt:

from math import*


print(sqrt(16))

output:
4.0p

ceil(x): This function will round the value to the integer greater than or equal to x
value.

ceil(10.5)

ceil(4.6)

ceil(4)

from math import*


print(ceil(10.5))

output:
11

output:
D:\>py [Link]
11

floor(x): This function will round the value to the integer less than or equal to x
value.

floor(10.5)

floor(5)

from math import*


print(floor(10.5))

Python 22 batch 7 pm Page 1


output:
D:\>py [Link]
10

fmod(x,y): This function will give remainder as a output.


The return type is a float data type value

from math import*


print(fmod(4,2))

output:
0.0

abs-->absolute
This function will give output without negative symbol.

from math import*


print(fabs(-12))

output:
12.0

Write a program to calculate area of a circle when radius of a circle is given


a=

pi=3.141592653589793

from math import*


r=int(input('enter the radius of a circle:'))
area=pi*pow(r,2)

print("Area of the circle is:",area)

D:\>py [Link]
enter the radius of a circle:4
Area of the circle is: 50.26548245743669

Random Module:

Python 22 batch 7 pm Page 2


To generate random values we will use random module, random module is a inbuilt
module

In random module there are 5 functions

[Link]()
[Link]()
[Link]()
[Link]()
[Link]()

[Link]()

random() is used to generate random float values between 0 and 1(not inclusive)

0<x<1

**It never generates 0 and it never generates 1

from random import*


print(random())

output:
D:\>py [Link]
0.13764545877139434

D:\>py [Link]
0.6645396333772283

D:\>py [Link]
0.317592306214567

D:\>py [Link]
0.06021067330547558

D:\>py [Link]
0.55280180355482

Python 22 batch 7 pm Page 3


D:\>py [Link]
0.29722681521014693

from random import*


for x in range(10):
print(random())

output:
D:\>py [Link]
0.6195619383673585
0.7650319773672067
0.5412240104114934
0.266770039235096
0.0333846858571627
0.6890254563976113
0.8336981460872318
0.2985762567369882
0.9394901124501817
0.45281382735622444

[Link]()

syntax:
uniform(begin,end)

This function generates random float values between begin and end

begin<x<end , it never generates begin value and end value

from random import*


print(uniform(1,7))

output:
D:\>py [Link]
5.5272825265391345

D:\>py [Link]
6.768569412098124

Python 22 batch 7 pm Page 4


D:\>py [Link]
6.357483120720277

D:\>py [Link]
4.32691156112012

from random import*


for x in range(5):
print(uniform(3,4))

output:
D:\>py [Link]
3.144048624311151
3.664401110815753
3.25814828067614
3.7498779679331617
3.940810141652418

[Link]()

randint(begin,end)

This function generates random integer values both(begin,end) values will be


included

from random import*


print(randint(1,10))

output:
D:\>py [Link]
9

D:\>py [Link]
8

D:\>py [Link]
2

D:\>py [Link]
Python 22 batch 7 pm Page 5
D:\>py [Link]
9

D:\>py [Link]
5

D:\>py [Link]
10

[Link]()

randrange(begin,end,step)

*begin value is optional , if begin value is not given then default value is 0.
*step value is optional, if not given then default value is 1

It will generates the values between begin and end-1

from random import*


print(randrange(5))

begin value-->0
step value-->1

output:
D:\>py [Link]
1

D:\>py [Link]
1

D:\>py [Link]
4

D:\>py [Link]
2

from random import*


print(randrange(1,5)) [1,2,3,4]

Python 22 batch 7 pm Page 6


from random import*
print(randrange(1,5)) [1,2,3,4]
D:\>py [Link]
2

D:\>py [Link]
4

D:\>py [Link]
4

D:\>py [Link]
2

from random import*


print(randrange(1,11,2))

[1,3,5,7,9]
D:\>py [Link]
1

D:\>py [Link]
1

D:\>py [Link]
1

D:\>py [Link]
7

choice():

choice is applicable for list,tuple,string.

syntax:

choice(list/tuple/string)

from random import*


fruits=['apple','banana','orange','grapes']
Python 22 batch 7 pm Page 7
fruits=['apple','banana','orange','grapes']
print(choice(fruits))

output:
D:\>py [Link]
grapes

D:\>py [Link]
apple

D:\>py [Link]
apple

D:\>py [Link]
banana

from random import*


fruits=('apple','banana','orange','grapes')
print(choice(fruits))

output:
D:\>py [Link]
apple

D:\>py [Link]
grapes

D:\>py [Link]
orange

from random import*


name='apple'
print(choice(name))

output:
D:\>py [Link]
p

D:\>py [Link]
l
Python 22 batch 7 pm Page 8
l

D:\>py [Link]
p

D:\>py [Link]
a

D:\>py [Link]
a

Python 22 batch 7 pm Page 9

Common questions

Powered by AI

The random module functions play a vital role in probabilistic simulation by providing diverse methods to generate random numbers across both numerical and categorical domains. Functions like random(), randint(), and choice() enable the creation of stochastic models that approximate real-world randomness and variability, essential in fields such as statistical mechanics or financial modeling .

Including both start and step parameters in randrange() allows for precise control over the sequence of generated numbers, tailoring random value selection within specific intervals. This functionality is significant for applications requiring interval-specific randomness, such as creating sequences that simulate time-steps or structural patterns .

The ceil() function rounds a number up to the nearest integer that is greater than or equal to the number, as shown by ceil(10.5) which results in 11. The floor() function rounds a number down to the nearest integer that is less than or equal to the number, demonstrated by floor(10.5) which results in 10 .

The randint(begin, end) function is unique because it includes both endpoints in generating random integer values, unlike other functions which typically exclude endpoints. This characteristic makes it particularly useful for simulations or applications where inclusive boundary values are crucial for modeling real-world phenomena .

The choice() function increases flexibility by allowing the selection of random elements from lists, tuples, or strings without any type-conversion process. This adaptability is crucial for programs where randomization of content from various data types is needed, enabling diverse applications such as shuffling or load distribution in algorithms .

Using pow(r,2) in Python simplifies the expression by directly calculating the square of the radius, improving readability and precision in mathematical operations. This is shown in the example where area = pi*pow(r,2) yields the area of the circle with precision due to the inherent nature of the pow function .

The choice() function facilitates data-driven decision-making by enabling random selection from iterable containers, thus supporting algorithms where non-deterministic processes are essential. It is particularly useful in genetic algorithms and Monte Carlo simulations where decision trees or paths rely on probabilistic divergence based on data distribution .

The random() function generates random float values in the interval (0, 1), meaning it never includes 0 or 1 in its output. This characteristic ensures uniform distribution strictly within the open interval, allowing for unbiased random data generation without endpoints, which is essential in simulations and stochastic processes .

The uniform(begin, end) function generates random float values between the specified begin and end values, excluding both endpoints, whereas random() generates values strictly between 0 and 1. Uniform behavior allows for span-specific random float generation suitable for custom range specifications .

The fmod(x, y) function returns the remainder of the division of x by y as a float data type, which differentiates it from the modulus operator (%) that returns an integer. This is significant when precise float division remainders are needed, as evident from fmod(4, 2) returning 0.0 .

You might also like