Random Module in Python

In this lesson we want to talk about Random Module in Python, first of all let’s talk about Python.

 

 

What is Python?

Python is high level, interpreted and general purpose programming language. it was created in the late 1980s by Guido van Rossum and was first released in 1991. Python is simple and it has clear syntax,  Python is dynamically typed language, it means that you don’ need to declare the data type of variable before using it, also in Python we have bject oriented, procedural and functional programming. you can use Python in different types of applications like web development, scientific computing, data analysis, artificial intelligence and many more.

 

 

Python Random Module

Random module in Python provides different functions for generating random numbers and performing other random operations. some of the commonly used functions in the random module are:

Method Description
random.random() Generates a random float between 0.0 and 1.0.
random.randint(a, b) Generates a random integer between a and b, inclusive.
random.choice(sequence) Chooses a random element from the given sequence, such as a list or tuple.
random.shuffle(sequence) Shuffles the elements of the given sequence in place.
random.sample(population, k) Returns a list of k unique elements randomly selected from the given population.

 

 

 

This is an example of using random module for generating a random number between 1 and 6, simulating the roll of a dice:

In this example random.randint function is used to generate a random integer between 1 and 6. After that the result is printed to the console.

 

 

 

This is the result

Random Module in Python
Random Module in Python

 

 

 

 

 

It’s important to note that the random numbers generated by random module are not truly random, but they are instead generated using a deterministic algorithm known as a pseudo-random number generator. For many use cases, this is sufficient, but for applications that require truly random numbers, such as cryptography, a different method may be necessary.

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×